- Blog

Keep it simple

[MySQL] Swapping ids of two Doctrine ORM entities with foreign key constraints

written on 9 September 2017
I recently had to swap the ids of two Doctrine ORM entities (pages), that were referenced by other entities (articles). Usually you don't want to do this at all. Even copying the entity contents over would be cleaner. However, I had to achieve the result with only MySQL. You can first disable the check for foreign constraints check (and re-enable it afterwards!). Then, swap the ids of the entities using a temporary id that is >= AUTO_INCREMENT to avoid UNIQUE constraint error. Afterwards, fix the references with a single UPDATE call using the SWITCH statement. The following code swaps the ids of two pages #12 and #13 and updates references in articles: ```sql SET foreign_key_checks = 0; -- Swap ids, where 999999 is >= current AUTO_INCREMENT UPDATE pages SET id=999999 WHERE id=12; UPDATE pages SET id=12 WHERE id=13; UPDATE pages SET id=13 WHERE id=999999; -- Updates references in articles with single UPDATE UPDATE articles SET page_id = CASE page_id WHEN 12 THEN 13 WHEN 13 THEN 12 END WHERE page_id IN (12,13); SET foreign_key_checks = 1; ```

Fixing Windows not booting (blinking cursor) without second drive

written on 3 June 2017
Had a simple Windows setup where Windows was installed on the SSD + extra Data HDD. I recently took the HDD out of this system and it didn't boot anymore. Just a blinking cursor. Start repair from windows setup couldn't fix the problem. I then went ahead and fixed it manually by finding and removing a property called `resumeobject` in the BCD (Windows Boot Configuration Data) entry. I suspected this object to refer to a non-existent file. To delete this `resumeobject` entry: 1. Boot from Windows install medium, go into command line 2. You can now list your entries in the BCD: ```powershell bcdedit /enum ``` 3. To delete the `resumeobject` property: ```powershell bcdedit /deletevalue {default} resumeobject ``` Here, `{default}` refers to the respective entry in the list you got above. Note that the boot manager files may have been also installed on the second drive for some reason. You can recreate them on the main drive (with the Windows files on it), say the `C:` drive: ```powershell bcdboot C:\Windows /s C: /f ALL ``` Here, `/f ALL` specifies that all firmware types are supported. Check out `bcdboot /?` for all possible options.

Linearizing depth buffer samples in HLSL

written on 31 March 2017
In Direct3D, we can can create a shader resource view (SRV) for our hardware depth buffer to sample it in the shader. Usually you will use something like `DXGI_FORMAT_D32_FLOAT` for the depth stencil view and `DXGI_FORMAT_R32_FLOAT` for the SRV. However, this means that we get non-linear depth when sampling in the shader: ``` // The projection done in the vertex shader and perspective divide float4 clipCoords = mul(mtxProj, mul(mtxView, worldCoords)); clipCoords /= clipCoords.w; // [-1,1] -> [0,1] float2 normClipCoords = clipCoords.xy * 0.5f + 0.5f; // Sample the raw depth buffer float nonLinearDepth = depthBuffer.Sample(pointSampler, normClipCoords).r; ``` The sampled depth is in range `[0,1]` where 0 represents the near- and 1 the far-clip-plane. Additionally, the distribution is not linear, but changes in depth close to the near-clip-plane have a higher resolution than changes far away from the camera. **To linearize the sampled depth-buffer value**, we can multiply the native device coordinates (ndc) vector by the inverse projection matrix and divide the result by the w coordinate (as the result is a homogenous vector). ``` // We are only interested in the depth here float4 ndcCoords = float4(0, 0, nonLinearDepth, 1.0f); // Unproject the vector into (homogenous) view-space vector float4 viewCoords = mul(mtxProjInv, ndcCoords); // Divide by w, which results in actual view-space z value float linearDepth = viewCoords.z / viewCoords.w; ``` Note that based on your projection matrix, you may have to negate the resulting depth, as it may point into the camera.

Empty WSDL output of a WCF Service and response 400 Bad Request

written on 18 January 2017
I was running into this problem with a .NET WCF service (hosted with Windows Service Host) that had a custom binding endpoint and a host baseAddress configured in the app.config. The response to a GET request on `http://localhost:9001/myService/?wsdl` was a `400 - Bad Request`. In the Service Trace it said: > *“There is a problem with the XML that was received from the network.“* and > *“The body of the message cannot be read because it is empty”* The reason was that I forgot a trailing slash at the end of the base address: ```xml <endpoint contract=”...” binding=”...” bindingConfiguration=”...” /> <host> <baseAddresses> <add baseAddress="http://localhost:9001/myService" /> <!-- Wrong --> <add baseAddress="http://localhost:9001/myService/" /> <!-- Correct --> </baseAddresses> </host> ```

Delete dangling IPv6 address from VMnet8 adapter on windows

written on 28 December 2016
After changing the IPv6 prefix in the virtual network manager in VMWare Workstation, I ended up with a dangling ip address and I wondered how I could get rid of it. When trying `netsh delete address “VMware Network Adapter VMnet8” fd20:XXXXXX` it told me that the command was not found. **It turns out, you have to explicitly tell netsh that you’re talking about an ipv6 address:** ```powershell netsh int ipv6 delete address “VMware Network Adapter VMnet8” fd20:XXXXXXXX ``` Remember to run this on a cmd as an Administrator, otherwise you get a confusing error saying *“The system cannot find the file specified”*.

Starting with C# WCF SOAP services and EntityFramework: Lessons learned

written on 5 November 2016
I just started to develop a self-hosting WCF SOAP service using EntityFramework for serialization (with a MSSQL database backend). This service will later be accessed by a PHP SoapClient. Let me summarize some experiences here: * You can create C# WCF contracts from a WSDL by using: `svcutil your.wsdl` * Services are instanced for each request by default. You can change this by using following attribute on you service implementation: * `[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]` * This allows you to pass parameters to your service * To allow non-Administrator users to run the service, execute this on the cmd as an administrator (you can use `+` as your address to represent localhost): ```powershell netsh http add urlacl http://your-address:PORT/ user=USERNAME ``` * WCF requires a getter AND setter for all properties (you can make them protected though): * otherwise the service host answers the SOAP request with a RST packet, resulting in a error saying: `[HTTP] Error Fetching http headers` * To trace WCF events: <a href="" target="_blank">See MSDN</a> * EntityFramework (EF) can be installed in the NuGet packet manager and will be automatically installed when running your project on a different machine * Unfortunately, EntityFramework does not yet support an InMemory database for testing in the current stable version (EF6) * However, it will be supported in EF7

[Linux Teamspeak3 server] Using the serverquery to disable weblist or recover administrative access

written on 5 November 2016
The teamspeak server query over telnet uses an unencrypted channel to transmit the password. However, you can set a temporary serveradmin password to (e.g.) disable weblist of your virtual server: 1. Stop the server and inside your teamspeak server directory, run the server temporarily with a fixed serveradmin password: ```bash ./ serveradmin_password=PASSWORD ``` 2. Connect via telnet to your server on port 10011 (default) and login with the temporary password: ```bash login serveradmin PASSWORD ``` 3. To disable weblist on server 1: ```bash use sid=1 serveredit virtualserver_weblist_enabled=0 ``` 4. To generate a new access token: 1. Connect via teamspeak client and find out the number in parantheses behind the group name in `Permissions > Servergroups` 2. Create a new token via serverquery: ```bash tokenadd tokentype=0 tokenid1=<Group ID> tokenid2=0 ``` 5. Close telnet session, stop `` and run it again with a **random** PASSWORD 6. Stop the server and start the production server again 7. Clear out shell history entries containing the new random password