Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## version 2.3
- ## run this script with any parameter in terminal for help, like this:
- ## KSP_launcher.sh --help
- ## otherwise place it into the same directory where KSP binary exists, run.
- ## if you use GOG standalone installer, in start.sh replace strings "KSP.x86_64" and "KSP.x86", with "KSP_launcher.sh"
- ## you can also make the KSP desktop file invoke this script.
- ########################### values: >default< / possible
- ## debug setting.
- ## setting this variable to anything other than "off", will cause script to dump the launch string, instead of running ksp.
- KSP_debuglauncher="off"
- KSP_gallium_bench="off" ## on / >off<
- KSP_ksp_cleanprefs="on" ## >on< / off
- KSP_ksp_forceversion="off" ## >off< / 32 / 64
- KSP_ksp_logrotate="on" ## >on< / off , also see KSP_unity_logdisable
- KSP_ksp_protect="on" ## >on< / off
- KSP_mesa_disableDRI3="off" ## on / >off<
- KSP_mesa_noerror="off" ## on / >off<
- KSP_mesa_software="off" ## on / >off<
- KSP_mesa_vsync="on" ## >on< / off , on - enables vsync
- KSP_nv_opts="on" ## >on< / off
- KSP_nv_preload="off" ## on / >off<
- KSP_nv_vsync="on" ## >on< / off , on - enables vsync
- KSP_radeon_R600_DEBUG="off" ## >off< / clear / option(,option,..)
- KSP_unity_forceX="off" ## >off< / horizontal resolution, like 1920
- KSP_unity_forceY="off" ## >off< / vertical resolution, like 1080
- KSP_unity_forceLC="on" ## >on< / off
- KSP_unity_fullscreen="off" ## on / >off<
- KSP_unity_gl_old="off" ## on / >off<
- KSP_unity_glclamp="off" ## on / >off<
- KSP_unity_glcore="off" ## on / >off< / 32 / 33 / 40 / 41 / 42 / 43 / 44 / 45
- KSP_unity_gles="off" ## on / >off< / 20 / 30 / 31 / 31aep
- KSP_unity_glthread="off" ## on / >off<
- KSP_unity_logdisable="off" ## on / >off< , also see KSP_ksp_logrotate
- display_help() {
- echo 'written by Kerbal101. GPL3+ '
- echo ''
- echo '==============================================='
- echo 'KSP_gallium_bench '
- echo 'on / >off< '
- echo ''
- echo 'gallium-drivers only: radeon/amdgpu, nouveau, intel-gallium (not intel classic). '
- echo 'enable string to debug performance. outputs cpu, gpu, fps, gpu memory graphs. '
- echo ''
- echo '==============================================='
- echo 'KSP_ksp_cleanprefs '
- echo '>on< / off '
- echo ''
- echo 'cleans "prefs" file, which is unity configuration cache. '
- echo 'this file is automatically re-generated if absent, as unity runs its detection routine. '
- echo 'however, if it exists, unity will use it - instead of running detection, which will cause issues, '
- echo 'if video resolution is changed in the settings. its absolutely safe to enable. '
- echo ''
- echo '==============================================='
- echo 'KSP_ksp_forceversion '
- echo '>off< (autodetect) / 32 / 64 '
- echo ''
- echo 'you can force the 32 or 64 bit version here. "off" will use autodetection instead. '
- echo ''
- echo '==============================================='
- echo 'KSP_ksp_logrotate '
- echo '>on< / off '
- echo ''
- echo 'a log file is generated every time KSP starts and is a huge text file. '
- echo 'it is required for support, however it will be overwritten every time KSP is started. '
- echo 'with this option enabled, the script will backup and compress the old log with tar - keeping at least three compressed copies. '
- echo 'also see "KSP_unity_logdisable" to disable the log altogether, if needed. '
- echo ''
- echo '==============================================='
- echo 'KSP_ksp_protect '
- echo '>on< / off '
- echo ''
- echo 'enable crash protection, if KSP requests too much memory too fast. '
- echo 'this causes Linux to kill KSP instead of whole Xorg session, by rising KSP "naughtiness level". '
- echo 'you should really leave at "on". '
- echo ''
- echo '==============================================='
- echo 'KSP_mesa_disableDRI3 '
- echo 'on / >off< '
- echo ''
- echo 'mesa only: disables DRI3, which will cause driver to fallback to DRI2. '
- echo 'this will impact (worsen) performance and is used for debugging. '
- echo 'known to solve issue with game freezing at random points on intel driver. '
- echo 'https://bugs.freedesktop.org/show_bug.cgi?id=97890 '
- echo ''
- echo '==============================================='
- echo 'KSP_mesa_noerror'
- echo 'on / >off< '
- echo ''
- echo 'mesa only: disables error checking in mesa driver. '
- echo 'this may reduce CPU overhead, at the price of disabled error checks, which would crash the misbehaving game. '
- echo ''
- echo '==============================================='
- echo 'KSP_mesa_software '
- echo 'on / >off< '
- echo ''
- echo 'mesa only: disables hardware accelerated rendering completely and switches to LLVM-assisted software renderer. '
- echo 'usefull for debugging properties only, severely impacts performance. '
- echo ''
- echo '==============================================='
- echo 'KSP_mesa_vsync '
- echo '>on< / off '
- echo ''
- echo 'mesa only: disables vsync (off setting), which improves performance at cost of tearing and will cause GPU to run constantly hot. '
- echo 'you may want to disable SwapBuffersWait for DDX driver (in xorg.conf.d) too '
- echo 'see https://www.x.org/wiki/RadeonFeature/ '
- echo ''
- echo '==============================================='
- echo 'KSP_nv_opts '
- echo '>on< / off '
- echo ''
- echo 'nvidia proprietary only: force driver threaded optimizations by exporting a variable. '
- echo ''
- echo '==============================================='
- echo 'KSP_nv_preload '
- echo 'on / >off< '
- echo ''
- echo 'nvidia proprietary only: preloads the blob, which can solve some problems. '
- echo 'will interfere with benchmarking software like GLXOSD. '
- echo ''
- echo '==============================================='
- echo 'KSP_nv_vsync '
- echo '>on< / off '
- echo ''
- echo 'nvidia proprietary only: disables vsync (off) on nvidia proprietary driver. '
- echo ''
- echo '==============================================='
- echo 'KSP_radeon_R600_DEBUG '
- echo '>off< / clear / option(,option,..) '
- echo ''
- echo 'radeon/amdgpu driver only: "R600_DEBUG" is a variable for changing driver options of Radeon/RadeonSI/AMDGPU. '
- echo 'some options may be already enabled in the driver internally, these are marked with "enabled by default" below. '
- echo '"off" and "clear" are handled by the script and can′t be combined with others. '
- echo ''
- echo 'to get full list of options supported by your current driver, install glx-utils package and run this in terminal: '
- echo '$ R600_DEBUG=help glxgears '
- echo ''
- echo 'options can be combined using comma where it makes sense: '
- echo '"KSP_mesa_R600_DEBUG=sbcl,hyperz,llvm,sisched,forcedma" '
- echo ''
- echo 'off - (script) do not touch R600_DEBUG variable. '
- echo 'clear - (script) clear the R600_DEBUG variable, causing driver to run with default options. '
- echo ''
- echo 'Known options:'
- echo 'sb - enable shader optimizer. (enabled by default) '
- echo 'sbcl - enable shader optimizer with opencl improvements. '
- echo 'sbsafemaths - enable optimized shader backend but disable fast unsafe math ops. '
- echo 'nosb - disable shader backend. '
- echo 'hyperz - enable hyperz. (enabled by default) '
- echo 'nohyperz - disable hyperz. '
- echo 'forcedma - force enable dma. '
- echo 'nodma - disable dma. '
- echo 'nocpdma - disable CP dma. '
- echo 'nowc - disable write combining. '
- echo 'llvm - enable llvm backend for shader compilation. (enabled by default) '
- echo 'nollvm - disable llvm backend for shader compilation. '
- echo 'sisched - enable improved radensi scheduler in llvm. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_forceX '
- echo '>off< / horizontal resolution, like 1920 '
- echo 'KSP_unity_forceY '
- echo '>off< / vertical resolution, like 1080 '
- echo ''
- echo 'unity option: force horizontal and vertical resolution. '
- echo 'useful to if KSP starts with window of weird sizes. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_forceLC '
- echo '>on< / off '
- echo ''
- echo 'exports LC_ALL=C var, which solves the problem of non-us_US encoding with numeric comma. '
- echo 'symptons include absent savegames and KSP forgetting settings. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_fullscreen '
- echo 'on / >off<'
- echo ''
- echo 'unity option: force fullscreen in KSP. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_gl_old '
- echo 'on / >off< '
- echo ''
- echo 'unity option: force old unity OpenGL backend. blocks gles settings. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_glclamp '
- echo 'on / >off< '
- echo ''
- echo 'unity option: disable opengl extensions. primary used for debugging.'
- echo ''
- echo '==============================================='
- echo 'KSP_unity_glcore '
- echo 'on / >off< / 32 / 33 / 40 / 41 / 42 / 43 / 44 / 45 '
- echo ''
- echo 'unity option: force unity to use GL "core profile", instead of the "compat. profile" - which has legacy/outdated GL codepaths. '
- echo 'recommended on mesa and drivers that dont implement legacy functions fully. '
- echo 'enable if you experience weird planet colors or black screen during launch. '
- echo 'see: https://docs.unity3d.com/Manual/OpenGLCoreDetails.html '
- echo 'blocks gles settings. '
- echo 'off = use compat. profile, on = force and detect core profile, number = on +limit core profile to specified level'
- echo ''
- echo '==============================================='
- echo 'KSP_unity_gles '
- echo 'on / >off< / 20 / 30 / 31 / 31aep '
- echo ''
- echo 'unity option: force opengles. will not work, if glcore or oldgl options are enabled. '
- echo 'see: https://docs.unity3d.com/Manual/OpenGLCoreDetails.html '
- echo 'off = use opengl, on = use gles, number = on+limit gles profile to level'
- echo ''
- echo '==============================================='
- echo 'KSP_unity_glthread '
- echo 'on / >off< '
- echo ''
- echo 'unity option: force OpenGL backend to use single rendering thread. may reduce render performance. '
- echo 'this is known to solve issues with old amd proprietary driver (fglrx/catalyst) freezing, but is applicable to all drivers. '
- echo ''
- echo '==============================================='
- echo 'KSP_unity_logdisable '
- echo 'on / >off< '
- echo ''
- echo 'unity option: will disable generating the log file. log file is generated every time KSP starts and is a text file. '
- echo 'it is required for support, however it can quickly grow gigabytes in size and may introduce slowdowns. '
- echo 'setting this option to on will cause unity to stop writing the log file. '
- echo 'also see "KSP_ksp_logrotate". '
- echo ''
- echo -e 'If you want ability to scroll through this help in terminal, pipe it through "| less" : '
- echo -e "${0} --help | less \n"
- echo -e 'Use arrows to scroll and "q" to quit. '
- exit 0
- }
- ## this is an internal variable that is filled as needed. Do not change.
- KSP_params=""
- ## this is an internal variable that is filled with exported values, shown at debug. Do not change.
- KSP_env_debug=""
- ## just for deduplication
- KSP_playerlog="${HOME}/.config/unity3d/Squad/Kerbal Space Program/Player.log"
- ## display help if we have any parameters, else run.
- if [[ -n "$1" ]]; then display_help; fi
- echo "${0}: starting script."
- if [[ "$KSP_ksp_forceversion" = "32" ]]; then
- KSP_bin="./KSP.x86"
- elif [[ "$KSP_ksp_forceversion" = "64" ]]; then
- KSP_bin="./KSP.x86_64"
- else
- if [[ "$(uname -m)" = "x86_64" ]]; then
- KSP_bin="./KSP.x86_64"
- else
- KSP_bin="./KSP.x86"
- fi
- fi
- if [[ "$KSP_mesa_vsync" = "off" ]]; then
- export vblank_mode=0
- KSP_env_debug+="export vblank_mode=0\n"
- fi
- if [[ "$KSP_nv_vsync" = "off" ]]; then
- nvidia-settings --load-config-only --assign="SyncToVBlank=0"
- KSP_nvcorrect="yes"
- KSP_env_debug+="execute: nvidia-settings --load-config-only --assign=\"SyncToVBlank=0\"\n"
- fi
- ## options can be extracted with "GALLIUM_HUD=help glxgears"
- ## each option consumes cycles, so stat sparingly!
- if [[ ! "$KSP_gallium_bench" = "off" ]]; then
- export GALLIUM_HUD="VRAM-usage+temperature+fps+cpu+GPU-load,cpu0+cpu1+cpu2+cpu3+cpu4+cpu5+cpu6+cpu7"
- KSP_env_debug+="export GALLIUM_HUD=\"VRAM-usage+temperature+fps+cpu+GPU-load,cpu0+cpu1+cpu2+cpu3+cpu4+cpu5+cpu6+cpu7\"\n"
- fi
- if [[ ! "$KSP_mesa_noerror" = "off" ]]; then
- export MESA_NO_ERROR=1
- KSP_env_debug+="export MESA_NO_ERROR=1\n"
- fi
- if [[ "$KSP_ksp_cleanprefs" = "on" ]]; then
- rm -f "${HOME}/.config/unity3d/Squad/Kerbal Space Program/prefs"
- KSP_env_debug+="execute: rm -f \"${HOME}/.config/unity3d/Squad/Kerbal Space Program/prefs\"\n"
- fi
- if [[ "$KSP_unity_forceLC" = "on" ]]; then
- export LC_ALL=C
- KSP_env_debug+="export LC_ALL=C\n"
- fi
- if [[ ! "$KSP_mesa_disableDRI3" = "off" ]]; then
- export LIBGL_DRI3_DISABLE=1
- KSP_env_debug+="export LIBGL_DRI3_DISABLE=1\n"
- fi
- if [[ ! "$KSP_mesa_software" = "off" ]]; then
- export LIBGL_ALWAYS_SOFTWARE=1
- KSP_env_debug+="export LIBGL_ALWAYS_SOFTWARE=1\n"
- fi
- if [[ ! "$KSP_unity_fullscreen" = "off" ]]; then
- KSP_params+=" -screen-fullscreen 1"
- fi
- if [[ ! "$KSP_radeon_R600_DEBUG" = "off" ]]; then
- if [[ "$KSP_radeon_R600_DEBUG" = "clear" ]]; then
- unset R600_DEBUG
- KSP_env_debug+="unset R600_DEBUG\n"
- else
- unset R600_DEBUG
- export R600_DEBUG="${KSP_radeon_R600_DEBUG}"
- KSP_env_debug+="unset R600_DEBUG\n"
- KSP_env_debug+="export R600_DEBUG=\"${KSP_radeon_R600_DEBUG}\"\n"
- fi
- fi
- if [[ ! "$KSP_unity_forceX" = "off" ]]; then
- KSP_params+="-screen-width ${KSP_unity_forceX} "
- fi
- if [[ ! "$KSP_unity_forceY" = "off" ]]; then
- KSP_params+="-screen-height ${KSP_unity_forceY} "
- fi
- if [[ ! "$KSP_unity_gl_old" = "off" ]]; then
- KSP_params+="-force-opengl "
- fi
- if [[ ! "$KSP_unity_glthread" = "off" ]]; then
- KSP_params+="-force-gfx-direct "
- fi
- if [[ ! "$KSP_unity_glcore" = "off" ]]; then
- if [[ "$KSP_unity_glcore" = "on" ]]; then
- KSP_params+="-force-glcore "
- else
- KSP_params+="-force-glcore${KSP_unity_glcore} "
- fi
- fi
- if [[ ! "$KSP_unity_glclamp" = "off" ]]; then
- KSP_params+="-force-clamped "
- fi
- if [[ ! "$KSP_unity_gles" = "off" ]] && [[ "$KSP_unity_glcore" = "off" ]] && [[ "$KSP_unity_gl_old" == "off" ]]; then
- if [[ "$KSP_unity_gles" = "on" ]]; then
- KSP_params+="-force-gles "
- else
- KSP_params+="-force-gles${KSP_unity_gles} "
- fi
- fi
- if [[ ! "$KSP_nv_preload" = "off" ]]; then
- export LD_PRELOAD="libpthread.so.0 libGL.so.1"
- KSP_env_debug+="export LD_PRELOAD=\"libpthread.so.0 libGL.so.1\"\n"
- fi
- if [[ ! "$KSP_nv_opts" = "off" ]]; then
- export __GL_THREADED_OPTIMIZATIONS=1
- KSP_env_debug+="export __GL_THREADED_OPTIMIZATIONS=1\n"
- fi
- ### placement of the three blocks below is critical.
- ### first block checks if log is a link to /dev/null, and removes it if it is. Its repeated in the post-execute section.
- ### seconds checks for log files and backs them up. the previous action would remove an empty log.
- ### third block creates a block to /dev/null if specified, otherwise is ignored.
- ## unlinks the log file if its a symlink to void. it may be set to void by $KSP_unity_logdisable.
- if [[ $(readlink -- "$KSP_playerlog") = "/dev/null" ]]; then
- rm -f "$KSP_playerlog"
- fi
- if [[ ! "$KSP_ksp_logrotate" = "off" ]]; then
- ## "log option selected" level
- if $(type tar >/dev/null 2>&1); then
- ## "tar present" level
- if [[ ! "$KSP_debuglauncher" = "off" ]]; then
- ## "debug is running" level
- KSP_env_debug+="Would compress and rotate logs.\n"
- else
- ## "debug is not running" level
- if [[ -f "$KSP_playerlog" ]]; then
- ## "log file exists" level
- ## rotate the 2->3 (3 gets wiped in the process), 1->2 and generate 1 by compressing or copying.
- mv -f "${KSP_playerlog}-3.tar.gz" "${KSP_playerlog}-4.tar.gz" >/dev/null 2>&1
- mv -f "${KSP_playerlog}-2.tar.gz" "${KSP_playerlog}-3.tar.gz" >/dev/null 2>&1
- mv -f "${KSP_playerlog}-1.tar.gz" "${KSP_playerlog}-2.tar.gz" >/dev/null 2>&1
- tar -czf "${KSP_playerlog}-1.tar.gz" "$KSP_playerlog"
- rm -f "$KSP_playerlog"
- else
- ## "log file does not exist" level
- echo -e "${0}: Previous debug log not found, nothing to rotate.\n"
- fi
- fi
- else
- ## "tar not found" level
- echo -e "${0}: WARNING! Tar was not found on the system! KSP_ksp_logrotate disabled!\n"
- fi
- ## phew!
- fi
- ### errata: test have shown, that KSP 1.3 completely ignores this option as well as "-logFile"
- ### because of this, we are working around of this issue by using another method, which is symlinking the log to void.
- ### must remove the link at the end though, or "bad things may happen"(c)
- if [[ ! "$KSP_unity_logdisable" = "off" ]]; then
- if [[ ! "$KSP_debuglauncher" = "off" ]]; then
- KSP_env_debug+="Would disable the log, by linking it to /dev/null.\n"
- else
- ## KSP_params+="-nolog "
- ## KSP_params+="-logFile /dev/null"
- ln -s /dev/null "$KSP_playerlog"
- fi
- fi
- if [[ ! "$KSP_ksp_protect" = "off" ]]; then
- if [[ ! "$KSP_debuglauncher" = "off" ]]; then
- KSP_env_debug+="Would increase KSP binary naughtiness value.\n"
- else
- if [[ "$KSP_bin" = "./KSP.x86" ]]; then
- sh -c 'sleep 3 ; echo 15 > "/proc/$(pgrep KSP.x86)/oom_adj"' &
- sh -c 'sleep 2 ; echo 15 > "/proc/$(pgrep KSP.x86)/oom_adj"' &
- sh -c 'sleep 1 ; echo 15 > "/proc/$(pgrep KSP.x86)/oom_adj"' &
- else
- sh -c 'sleep 3 ; echo 15 > "/proc/$(pgrep KSP.x86_64)/oom_adj"' &
- sh -c 'sleep 2 ; echo 15 > "/proc/$(pgrep KSP.x86_64)/oom_adj"' &
- sh -c 'sleep 1 ; echo 15 > "/proc/$(pgrep KSP.x86_64)/oom_adj"' &
- fi
- fi
- fi
- ####### executing KSP if debug is off, otherwise just spill the parameters into shell
- if [[ ! "$KSP_debuglauncher" = "off" ]]; then
- echo -e "${KSP_env_debug}"
- echo -e "Would finally execute: ${KSP_bin} ${KSP_params}"
- exit 0
- else
- ## this construction is necessary, because KSP often segfaults instead of exiting, causing it to abort the script too.
- exec "$KSP_bin" "$KSP_params" &
- wait
- echo "${0}: KSP finished, performing post-exec actions."
- fi
- #######
- ####### post-execution block
- ## will restore vsync for nvidia after game exit, if the script enabled it.
- if [[ $KSP_nvcorrect = "yes" ]]; then
- nvidia-settings --load-config-only --assign="SyncToVBlank=1"
- fi
- ## unlinks the log file if its a symlink to void.
- if [[ $(readlink -- "$KSP_playerlog") = "/dev/null" ]]; then
- rm -f "$KSP_playerlog"
- fi
- echo "${0}: finished."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement