How to build a Factorio Dedicated Windows 7 Virtual Machine

Find multiplayer games.
Tools/scripts to run a dedicated server.
nsanity
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Mar 28, 2016 11:47 pm
Contact:

How to build a Factorio Dedicated Windows 7 Virtual Machine

Post by nsanity »

How to build a Factorio Dedicated Windows 7 Virtual Machine
Assumes

* you have the Steam version of the game for Windows
* you have a spare Windows 7 license to use for the virtual machine
* you can follow instructions - i'm not supporting this if you can't get it to work.
* you are comfortable with the command line - If you can't change directories, you're going to have a bad time.

System Requirements

* Some form of Hypervisor - Hyper-V, VMWare Player/Workstation, Virtual Box - basically anything that will run Windows 7
* You know your Steam Login details
* Factorio is *really* sensitive netcode wise. If *any* player connected lags, you all lag. It sucks, but if you have friends with shitty internet, I wouldn't invite them to your games. That said, I've played with 5 players (2 lan, 1 in Sydney, 1 in Melbourne) for 20-30 hours at a time just fine. The more players you have, the more upstream *ALL* players need (not just the host). So long as everyone is under 120ms to eachother, with a *stable* ping and ADSL2 upload, you should be fine.
* I built this on my 5930K X99-A PCIE M2 SSD system. Performance wise, YMMV. If in doubt give it more ram and place the VM on an SSD.

Disclaimer

This is not a secure VM.
This is not a particularly well scripted VM.
This is not a hardened install of Windows 7.

If it gets crypto'd, hacked, or whatever its not my fault. I built this VM in about 15 minutes one day when the local server multiplayer started running like shit.
I spent about an hour scripting the update stuff and its not code i'm particularly proud of. In fact it took me twice as long as all of that to write this post.

You can probably make it better - hell I'll probably do it. But its a starting point for you.

If this explodes in a firey mess and eats your dog, I apologise but i accept no liability.


Can't you script most of this?
Well yes - but you really shouldn't take people's scripts from the internet that involve installing applications, using your SteamID details, configuring firewalls, etc and just run them.
Its a bad idea. And a good way to have your stuff owned.

Step 1 - Build your VM

1. After creating my virtual machine, I just used a Vanilla Windows 7 Pro SP1 x64 install - nothing special here. For ease of uses sake, I called my VM

Code: Select all

Factorio
with the username "Factorio" - I use a password for reasons i'll highlight later.
2. Install your Hypervisor's suggested drivers/tools (e.g VMWare Tools, Hyper-V Integration Services, etc)
3. (Optional) After you login, setup autologin to windows - you can do this via
1. Click Start, type netplwiz, and then press Enter.
2. In the User Accounts dialog box, click the account you want to automatically log on to.If it is available, clear the Users Must Enter A User Name And Password To Use This Computer check box.
3. Click OK.
4. In the Automatically Log On dialog box, enter the user’s password twice and click OK.
Note i didn't bother updating my VM because i was never going to publish it to the web.
4. (Optional) Give your VM a static IP / DHCP Reservation for easier router forwarding
5. I turned UAC off and set run all applications as admin on - you can do this via;
1. Run - >

Code: Select all

%windir%\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
Accept the UAC prompt

2. Run - >

Code: Select all

%windir%\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f
Accept the UAC prompt

3. Run ->

Code: Select all

shutdown -r -t 0 -f

Step 2 - Install SteamCMD, and connect to your account

https://developer.valvesoftware.com/wiki/SteamCMD

1. Download SteamCMD from here - https://steamcdn-a.akamaihd.net/client/ ... eamcmd.zip
2. I install all of SteamCMD into c:\steamcmd
3. Open a command prompt and change directory to c:\steamcmd
4. Launch SteamCMD and auth your steamguard to this pc

Code: Select all

steamcmd.exe
login <your steam username>
<your steam password>
This will trip your steamguard - you need to go get the 6 alphanumeric key from your email and punch it in

Code: Select all

<your steamguard ID emailed to you by Valve>
Don't quit Steamcmd yet...

Step 3 - Download Factorio to your VM

Still within SteamCMD now type

Code: Select all

force_install dir c:\factorio
app_update 427520 validate
This will download the Factorio client (370MB or thereabouts) - it may take a while, after its finished - then quit steamcmd

Code: Select all

quit

Step 4 - Configure Factorio Game launches

1. Browse to C:\ and make a folder called "scripts"
2. Download this application (SendSignalCtrlC.exe) from dropbox - https://dl.dropboxusercontent.com/u/490 ... lCtrlC.exe (mirror https://dl.dropboxusercontent.com/u/490 ... lCtrlC.exe)
This file will send a Ctrl+C to a process - allowing Factorio to save the game and quit gracefully. I did not write/compile this script - it was found in this thread http://stackoverflow.com/questions/8130 ... er.com/web Author source is here - http://www.latenighthacking.com/project ... endSignal/
3. Save the following scripts in the c:\scripts folder

NOTE YOU WILL NEED TO UPDATE <your steam username> AND <your steam password> IN THE BELOW SCRIPT FOR IT TO WORK

update_factorio.cmd

Code: Select all

@tasklist /FI "IMAGENAME eq factorio.exe" 2>NUL | find /I /N "factorio.exe">NUL
if "%ERRORLEVEL%"=="0" GOTO :CLOSE_FACTORIO 
if "%ERRORLEVEL%"=="1" GOTO :UPDATE_FACTORIO

:CLOSE_FACTORIO
@ECHO CLOSE_FACTORIO
@echo off
for /f "tokens=2 delims=," %%F in ('tasklist /nh /fi "imagename eq factorio.exe" /fo csv') do set factoriopid=%%~F
@echo on
start /w c:\scripts\SendSignalCtrlC64.exe %factoriopid%
tasklist /FI "IMAGENAME eq factorio.exe" 2>NUL | find /I /N "factorio.exe">NUL
if "%ERRORLEVEL%"=="0" GOTO :CLOSE_FACTORIO
icacls %appdata%\Factorio\saves\* /grant Everyone:F /t
GOTO :UPDATE_FACTORIO_RESTART

:UPDATE_FACTORIO
@ECHO UPDATE_FACTORIO
start /W c:\steamcmd\steamcmd.exe +login <your steam username> <your steam password> +force_install_dir c:\factorio +app_update 427520 validate +quit
GOTO :EOF

:UPDATE_FACTORIO_RESTART
@ECHO UPDATE_FACTORIO_RESTART
start /WAIT c:\steamcmd\steamcmd.exe +login <your steam username> <your steam password> +force_install_dir c:\factorio +app_update 427520 validate +quit
IF "%factariolan%"=="YES" start "explorer.exe" "C:\Users\Factorio\Desktop\Launch Last Game - LAN.lnk"
IF "%factariolan%"=="NO" start "explorer.exe" "C:\Users\Factorio\Desktop\Launch Last Game - WAN.lnk"

GOTO :END

:END
@ECHO ENDING
NOTE Factorio multiplayer is a weird beast, if you set --latency-ms too high the game will be terrible. The advise is to find the average ping of the furthest player, then add 40ms to it - update the next 2 scripts accordingly

find_newest_save_and_launch-lan.cmd

Code: Select all

@setlocal enableextensions enabledelayedexpansion
@setx /m factariolan "YES"
@echo off
cd \
cd %appdata%\factorio\saves
for /f "tokens=*" %%a in ('dir /b /od') do ( 
	set newest=%%a
	if "x!newest:~-4!"=="x.zip" (
        set newest=!newest:~0,-4!
	))
cd \
cd c:\factorio\bin\x64
echo f| factorio --start-server %newest% --autosave-interval 15 --autosave-slots 3 --latency-ms 40
find_newest_save_and_launch-wan.cmd

Code: Select all

@setlocal enableextensions enabledelayedexpansion
@setx /m factariolan "NO"
@echo off
cd \
cd %appdata%\factorio\saves
for /f "tokens=*" %%a in ('dir /b /od') do ( 
	set newest=%%a
	if "x!newest:~-4!"=="x.zip" (
        set newest=!newest:~0,-4!
	))
cd \
cd c:\factorio\bin\x64
echo f| factorio --start-server %newest% --autosave-interval 15 --autosave-slots 3 --latency-ms 140
find_and_quit_factorio_with_save.cmd

Code: Select all

:CLOSE_FACTORIO
@echo off
for /f "tokens=2 delims=," %%F in ('tasklist /nh /fi "imagename eq factorio.exe" /fo csv') do set factoriopid=%%~F
@echo on
c:\scripts\SendSignalCtrlC64.exe %factoriopid%
tasklist /FI "IMAGENAME eq factorio.exe" 2>NUL | find /I /N "factorio.exe">NUL
if "%ERRORLEVEL%"=="0" GOTO :CLOSE_FACTORIO
icacls %appdata%\Factorio\saves\* /grant Everyone:F /t
exit
create_shadowcopy.bat

Code: Select all

Wmic.exe shadowcopy call create ClientAccessible,"C:\"
4. Create the following shortcuts on your desktop (right click -> create shortcut)

Launch Last Game - LAN

Code: Select all

target - C:\scripts\find_newest_save_and_launch-lan.cmd < nul
Launch Last Game - WAN

Code: Select all

target - C:\scripts\find_newest_save_and_launch-wan.cmd < nul
Update Factorio

Code: Select all

target - C:\scripts\update_factorio.cmd < nul
Quit Factorio

Code: Select all

C:\scripts\find_and_close_factorio_with_save.cmd < nul
5. Create path shortcuts for yourself to useful folders

mods

Code: Select all

target - %appdata%\Factorio\mods
saves

Code: Select all

target - %appdata%\Factorio\saves
Step 5 - Configure Scheduled Tasks for windows
1. (Optional) Run Task Scheduler and setup an Update on Startup task
Run ->

Code: Select all

%windir%\system32\taskschd.msc /s
Right Click on Task Scheduler Library -> Create Task
General Tab
Name - Update Factorio
Use the following user account - NT AUTHORITY\SYSTEM - n.b click change user or group, search for SYSTEM, click ok
Run with highest privileges - check
Configure for - Windows 7
Triggers Tab
New -> Begin the task: -> At Startup, ensure Enabled is checked -> Ok
Actions Tab
New -> Start a Program -> Browse -> C:\scripts\update_factorio.cmd -> Ok
Settings Tab
Allow task to be run on demand - check
Stop the task if it runs longer than - check, 1 hour
If the running task does not end when requested, force it to stop - check
If the task is running, then the following rule applies: -> Do not start a new instance
Click Ok to save and close
2. (Optional) Run Task Scheduler and setup a Volume Shadow Copy task

Code: Select all

%windir%\system32\taskschd.msc /s
Right Click on Task Scheduler Library -> Create Task
General Tab
Name - Shadow Copy C
Use the following user account - NT AUTHORITY\SYSTEM - n.b click change user or group, search for SYSTEM, click ok
Run with highest privileges - check
Configure for - Windows 7
Triggers Tab
New -> Begin the task: -> On a schedule -> Check Daily -> Set time to rounded hour (e.g 8:00:00 AM) -> Recur every: 1 days -> Repeat Task every 1 Hour for a duration of Indefinitely -> ensure Enabled is checked -> Ok
Actions Tab
New -> Start a Program -> Browse -> C:\scripts\create_shadowcopy.bat -> Ok
Settings Tab
Allow task to be run on demand - check
Run task as soon as possible after a scheduled start is missed - check
If the task fails, restart every -> 15 minutes
Attempt to restart up to: -> 3 times
Stop the task if it runs longer than - check, 1 hour
If the running task does not end when requested, force it to stop - check
If the task is running, then the following rule applies: -> Do not start a new instance
Click Ok to save and close
Step 6 - Create File shares so you can copy save games and mods in and out of the server

1. Change Control Panel -> Network and Sharing Centre -> Advanced Sharing Settings -> Home or Work (Current Profile) to
Turn on Network Discovery
Turn on File and Printer Sharing
Turn off Public Folder sharing
Use 128-bit encryption
Turn off password protected sharing
Use user accounts and passwords to connect to other computers
Then restart the VM
2. In Windows Explorer, browse to "%appdata%\Factorio% - if this folder does not exist, create it
3. Create a Folder called "mods", share it to "everyone" with read/write privileges and set the NTFS permissions for "everyone" to "Full Control"
4. Create a Folder called "saves", share it to "everyone" with read/write privileges and set the NTFS permissions for "everyone" to "Full Control"
5. Test that you can browse from your PC to \\factorio\saves and \\factorio\mods

Step 7 - Lock down your Steam account password that is stored in plain text
Because Factorio hasn't published a dedicated server app to Steam, you need to login to an account to download/update Factorio. This means you've saved your Steam account password in plain text. To restrict access to it, you need to lock down your c:\scripts folder with NTFS security. This is not perfect. If someone has direct console access to your VM and logs into your Factorio user, they can read your steam password - but you have Steam Guard enabled right?
1. Browse to the C:\
Right Click Scripts -> Properties -> Security Tab -> Advanced
Click Change Permissions
Untick "Include inheritable permissions from this object's parent" -> Select Add
Select the Item "Users (FACTORIO\users)" -> Remove
Select the Item "Authenticated Users" -> Remove
Click Add -> Type Factorio -> Check names -> Ok -> tick "Full Control" "Allow" -> Ok
Click Ok
Click Ok
Click Ok
Step 8 - Open UDP Port 34197 for your Factorio Server
1. Open ports on the Factorio Server
Run -> cmd

Code: Select all

netsh advfirewall firewall add rule name="Factorio UDP In" program="C:\Factorio\bin\x64\Factorio.exe" service="factorioudp" description="Factorio UDP" enable=Yes profile=Private dir=IN localip=ANY remoteip=ANY localport=34197 remoteport=34197 protocol=UDP interface=ANY action=allow
netsh advfirewall firewall add rule name="Factorio UDP Out" program="C:\Factorio\bin\x64\Factorio.exe" service="factorioudp" description="Factorio UDP" enable=Yes profile=Private dir=OUT localip=ANY remoteip=ANY localport=34197 remoteport=34197 protocol=UDP interface=ANY action=allow
2. (Optional) Open port 34197 UDP on your Router to your Factorio Server so Internet players can connect - every router is different, check http://portforward.com/english/routers/port_forwarding/ or whatever.





Hokay, so if you've done all the above, you should have a fairly robust Factorio Dedicated Server. It will update when you reboot the VM or if you run the "Update Factorio" shortcut.

Step 9 - How to play (finally)

1. Start your server.

If you try to run it straight off the bat with either of the Launch Last Game shortcuts - it won't work. Those shortcuts search the Saves folder for the last game you saved (or the game autosaved), and start the server with that game. So copy in a current save to the \\factorio\saves folder on your network and off you go. I like to start a Multiplayer game on my own PC, saving the map until i get a decent enough start, then copying that save up to the server for everyone to pile in.

If you have mods - you have to ensure *everyone* has the same mods (name AND version) - otherwise they can't connect (nor can the server start) - so copy those into \\factorio\mods - and distribute them with your fellow factorians - remember it belongs in their %appdata%\factorio\mods folder.

Save games seem to upgrade within major versions (e.g version 12 games will upgrade ok).

2. Connect to your server - Start Factorio -> Play -> Multiplayer -> Connect to Game -> Factorio (if you're a LAN player. If you're an Internet player joining your friend put their web facing IP in there, they can find it via typing "what is my ip" into google.)
Post Reply

Return to “Multiplayer / Dedicated Server”