Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux gnome crash workaround #91

Draft
wants to merge 21 commits into
base: mod
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ae27d70
Factor out resolution/windowed/fullscreen code
drojf Oct 8, 2022
dd0d599
Simplify resolution and windowed/fullscreen handling
drojf Oct 8, 2022
0582100
Default to fullscreen if playerprefs does not specify mode
drojf Oct 8, 2022
97a3505
Improve playerprefs printing
drojf Oct 8, 2022
f33743c
Fix SetPlayerPrefs() not working properly in ScreenManagerFix()
drojf Oct 8, 2022
2cd63bf
Make resolution buttons set only resolution and not toggle windowed s…
drojf Oct 8, 2022
d02b1b9
Show toast when resolution changed by user
drojf Oct 8, 2022
80eef05
Make the 'fullscreen' button always clickable because it now toggles …
drojf Oct 8, 2022
cd219d7
Force fullscreen if crash or corrupted playerprefs detected
drojf Oct 8, 2022
87d256d
Fix Windows incorrectly saving playerprefs and corrupted playerprefs …
drojf Oct 9, 2022
6693e19
Add support for multiple submenus (as a stack)
drojf Oct 9, 2022
c765743
Automatically show submenu when it is pushed onto menu stack
drojf Oct 10, 2022
2d0dbb8
Add playerprefs to lock game in fullscreen mode
drojf Oct 10, 2022
dc72f91
Add fullscreen lock setup menu
drojf Oct 12, 2022
dbeb177
Only show windowed setup menu on Linux
drojf Oct 12, 2022
bdb860b
Add button to reset fullscreen resolution
drojf Oct 12, 2022
33e0009
Set default width to 853 instead of 640
drojf Oct 29, 2022
903eb5d
Fix bug where windowed resolution isn't set correctly
drojf Oct 29, 2022
f55e8e4
Improve windowed mode test instructions
drojf Oct 29, 2022
dc53934
Add buttons to show/get registry path of playerprefs
drojf Oct 30, 2022
ed983fa
Add note to restart game when changing to windowed on Linux
drojf Oct 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
<Compile Include="MOD.Scripts.Core\MODLogger.cs" />
<Compile Include="MOD.Scripts.Core\MODSystem.cs" />
<Compile Include="MOD.Scripts.Core\MODUtility.cs" />
<Compile Include="MOD.Scripts.Core\MODWindowManager.cs" />
<Compile Include="MOD.Scripts.UI.ChapterJump\MODChapterJumpController.cs" />
<Compile Include="MOD.Scripts.UI.Tips\MODTipsController.cs" />
<Compile Include="MOD.Scripts.UI\MODCustomFlagPreset.cs" />
Expand All @@ -250,6 +251,7 @@
<Compile Include="MOD.Scripts.UI\MODActions.cs" />
<Compile Include="MOD.Scripts.UI\MODMenuAudioOptions.cs" />
<Compile Include="MOD.Scripts.UI\MODMenuAudioSetup.cs" />
<Compile Include="MOD.Scripts.UI\MODSubMenuWindowSetup.cs" />
<Compile Include="MOD.Scripts.UI\MODMenuSupport.cs" />
<Compile Include="MOD.Scripts.UI\MODMenuModuleInterface.cs" />
<Compile Include="MOD.Scripts.UI\MODMenuNormal.cs" />
Expand Down
19 changes: 14 additions & 5 deletions Assets.Scripts.Core.Buriko/BurikoScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,19 @@ public BurikoVariable OperationMODClearArtsets()
return BurikoVariable.Null;
}

private void ShowSetupMenuIfRequired()
{
if (MODAudioSet.Instance.HasAudioSetsDefined() && !MODAudioSet.Instance.GetCurrentAudioSet(out _))
{
GameSystem.Instance.MainUIController.modMenu.PushSubMenuAndShow(ModSubMenu.AudioSetup);
}

if (!MODWindowManager.FullscreenLockConfigured())
{
GameSystem.Instance.MainUIController.modMenu.PushSubMenuAndShow(ModSubMenu.WindowSetup);
}
}

public BurikoVariable OperationMODGenericCall()
{
SetOperationType("MODGenericCall");
Expand All @@ -2784,11 +2797,7 @@ public BurikoVariable OperationMODGenericCall()
switch(callID)
{
case "ShowSetupMenuIfRequired":
if(MODAudioSet.Instance.HasAudioSetsDefined() && !MODAudioSet.Instance.GetCurrentAudioSet(out _))
{
GameSystem.Instance.MainUIController.modMenu.SetSubMenu(ModSubMenu.AudioSetup);
GameSystem.Instance.MainUIController.modMenu.Show();
}
ShowSetupMenuIfRequired();
break;

case "LipSyncSettings":
Expand Down
16 changes: 1 addition & 15 deletions Assets.Scripts.Core.State/StateNormal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,7 @@ public bool InputHandler()
// Fullscreen
if (Input.GetKeyDown(KeyCode.F))
{
if (GameSystem.Instance.IsFullscreen)
{
int num14 = PlayerPrefs.GetInt("width");
int num15 = PlayerPrefs.GetInt("height");
if (num14 == 0 || num15 == 0)
{
num14 = 640;
num15 = 480;
}
GameSystem.Instance.DeFullscreen(width: num14, height: num15);
}
else
{
GameSystem.Instance.GoFullscreen();
}
MODWindowManager.FullscreenToggle(showToast: true);
}

// Toggle Language
Expand Down
178 changes: 12 additions & 166 deletions Assets.Scripts.Core/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ public class GameSystem : MonoBehaviour

private int SystemInit = 1;

private Resolution fullscreenResolution;

private int screenModeSet = -1;

private Stack<StateEntry> stateStack = new Stack<StateEntry>();

public bool HasFocus;
Expand All @@ -178,13 +174,6 @@ public class GameSystem : MonoBehaviour

// Unity will attempt to deserialize public properties and these aren't in the AssetBundle,
// so use private ones with public accessors
private bool _isFullscreen;
public bool IsFullscreen
{
get => _isFullscreen;
private set => _isFullscreen = value;
}

private float _configMenuFontSize = 0;
public float ConfigMenuFontSize
{
Expand Down Expand Up @@ -247,34 +236,17 @@ private void Initialize()
{
PlayerPrefs.SetInt("height", 720);
}
if (PlayerPrefs.GetInt("width") < 640)
if (PlayerPrefs.GetInt("width") < 853)
{
PlayerPrefs.SetInt("width", 640);
PlayerPrefs.SetInt("width", 853);
}
if (PlayerPrefs.GetInt("height") < 480)
{
PlayerPrefs.SetInt("height", 480);
}
IsFullscreen = PlayerPrefs.GetInt("is_fullscreen", 0) == 1;
fullscreenResolution.width = 0;
fullscreenResolution.height = 0;
fullscreenResolution = GetFullscreenResolution();

if (IsFullscreen)
{
Screen.SetResolution(fullscreenResolution.width, fullscreenResolution.height, fullscreen: true);
}
else if (PlayerPrefs.HasKey("height") && PlayerPrefs.HasKey("width"))
{
int width = PlayerPrefs.GetInt("width");
int height = PlayerPrefs.GetInt("height");
Debug.Log("Requesting window size " + width + "x" + height + " based on config file");
Screen.SetResolution(width, height, fullscreen: false);
}
if ((Screen.width < 640 || Screen.height < 480) && !IsFullscreen)
{
Screen.SetResolution(640, 480, fullscreen: false);
}
MODWindowManager.GameSystemInitSetResolution();

Debug.Log("Starting compile thread...");
CompileThread = new Thread(CompileScripts)
{
Expand Down Expand Up @@ -326,12 +298,7 @@ public void PostLoading()
public void UpdateAspectRatio(float newratio)
{
AspectRatio = newratio;
if (!IsFullscreen)
{
int width = Mathf.RoundToInt((float)Screen.height * AspectRatio);
Screen.SetResolution(width, Screen.height, fullscreen: false);
}
PlayerPrefs.SetInt("width", Mathf.RoundToInt(PlayerPrefs.GetInt("height") * AspectRatio));
MODWindowManager.RefreshWindowAspect();
MainUIController.UpdateBlackBars();
SceneController.UpdateScreenSize();
}
Expand Down Expand Up @@ -816,55 +783,14 @@ public void UpdateWaits()
});
}

public IEnumerator FrameWaitForFullscreen(int width, int height, bool fullscreen)
{
yield return (object)new WaitForEndOfFrame();
yield return (object)new WaitForFixedUpdate();
IsFullscreen = fullscreen;
PlayerPrefs.SetInt("is_fullscreen", fullscreen ? 1 : 0);
Screen.SetResolution(width, height, fullscreen);
while (Screen.width != width || Screen.height != height)
{
yield return (object)null;
}
}

public void GoFullscreen()
{
IsFullscreen = true;
PlayerPrefs.SetInt("is_fullscreen", 1);
Resolution resolution = GetFullscreenResolution();
Screen.SetResolution(resolution.width, resolution.height, fullscreen: true);
Debug.Log(resolution.width + " , " + resolution.height);
PlayerPrefs.SetInt("fullscreen_width", resolution.width);
PlayerPrefs.SetInt("fullscreen_height", resolution.height);
}

public void DeFullscreen(int width, int height)
{
IsFullscreen = false;
PlayerPrefs.SetInt("is_fullscreen", 0);
Screen.SetResolution(width, height, fullscreen: false);
}

private void OnApplicationFocus(bool focusStatus)
{
HasFocus = focusStatus;
}

private void LateUpdate()
{
if (screenModeSet == -1)
{
screenModeSet = 0;
fullscreenResolution = Screen.currentResolution;
if (PlayerPrefs.HasKey("fullscreen_width") && PlayerPrefs.HasKey("fullscreen_height") && Screen.fullScreen)
{
fullscreenResolution.width = PlayerPrefs.GetInt("fullscreen_width");
fullscreenResolution.height = PlayerPrefs.GetInt("fullscreen_height");
}
Debug.Log("Fullscreen Resolution: " + fullscreenResolution.width + ", " + fullscreenResolution.height);
}
MODWindowManager.GetFullScreenResolutionLateUpdate();
}

private bool CheckInitialization()
Expand Down Expand Up @@ -1008,6 +934,11 @@ private void OnApplicationQuit()
{
SteamController.Close();
}

if(CanExit)
{
MODWindowManager.OnApplicationReallyQuit("GameSystem.OnApplicationQuit()");
}
}

/// <summary>
Expand All @@ -1028,80 +959,6 @@ public T ChooseJapaneseEnglish<T>(T japanese, T english)
}
}

public Resolution GetFullscreenResolution()
{
Resolution resolution = new Resolution();
string source = "";
// Try to guess resolution from Screen.currentResolution
if (!Screen.fullScreen || Application.platform == RuntimePlatform.OSXPlayer)
{
resolution.width = this.fullscreenResolution.width = Screen.currentResolution.width;
resolution.height = this.fullscreenResolution.height = Screen.currentResolution.height;
source = "Screen.currentResolution";
}
else if (this.fullscreenResolution.width > 0 && this.fullscreenResolution.height > 0)
{
resolution.width = this.fullscreenResolution.width;
resolution.height = this.fullscreenResolution.height;
source = "Stored fullscreenResolution";
}
else if (PlayerPrefs.HasKey("fullscreen_width") && PlayerPrefs.HasKey("fullscreen_height"))
{
resolution.width = PlayerPrefs.GetInt("fullscreen_width");
resolution.height = PlayerPrefs.GetInt("fullscreen_height");
source = "PlayerPrefs";
}
else
{
resolution.width = Screen.currentResolution.width;
resolution.height = Screen.currentResolution.height;
source = "Screen.currentResolution as Fallback";
}

// Above can be glitchy on Linux, so also check the maximum resolution of a single monitor
// If it's bigger than that, then switch over
// Note that this (from what I can tell) gives you the biggest resolution of any of your monitors,
// not just the one the game is running under, so it could *also* be wrong, which is why we check both methods
if (Screen.resolutions.Length > 0)
{
int index = 0;
Resolution best = Screen.resolutions[0];
for (int i = 1; i < Screen.resolutions.Length; i++)
{
if (Screen.resolutions[i].height * Screen.resolutions[i].width > best.height * best.width)
{
best = Screen.resolutions[i];
index = i;
}
}
if (best.width <= resolution.width && best.height <= resolution.height) {
resolution = best;
source = "Screen.resolutions #" + index;
}
}
if (!PlayerPrefs.HasKey("fullscreen_width_override"))
{
PlayerPrefs.SetInt("fullscreen_width_override", 0);
}
if (!PlayerPrefs.HasKey("fullscreen_height_override"))
{
PlayerPrefs.SetInt("fullscreen_height_override", 0);
}

if (PlayerPrefs.GetInt("fullscreen_width_override") > 0)
{
resolution.width = PlayerPrefs.GetInt("fullscreen_width_override");
source += " + Width Override";
}
if (PlayerPrefs.GetInt("fullscreen_height_override") > 0)
{
resolution.height = PlayerPrefs.GetInt("fullscreen_height_override");
source += " + Height Override";
}
Debug.Log("Using resolution " + resolution.width + "x" + resolution.height + " as the fullscreen resolution based on " + source + ".");
return resolution;
}

/// <summary>
/// Gets the amount you should offset gui elements to center them properly based on the current aspect ratio.
/// Add this number to GUI elements' positions to center them, subtract it from window positions.
Expand All @@ -1113,18 +970,7 @@ public float GetGUIOffset() {

~GameSystem()
{
// Fixes an issue where Unity would write garbage values to its saved state on Linux
// If we do this while the game is running, Unity will overwrite the values
// So do it in the finalizer, which will run as the game quits and the GameSystem is deallocated
if (PlayerPrefs.HasKey("width") && PlayerPrefs.HasKey("height"))
{
int width = PlayerPrefs.GetInt("width");
int height = PlayerPrefs.GetInt("height");
PlayerPrefs.SetInt("Screenmanager Resolution Width", width);
PlayerPrefs.SetInt("Screenmanager Resolution Height", height);
PlayerPrefs.SetInt("is_fullscreen", IsFullscreen ? 1 : 0);
PlayerPrefs.SetInt("Screenmanager Is Fullscreen mode", 0);
}
MODWindowManager.OnApplicationReallyQuit("GameSystem Destructor");
}

static GameSystem()
Expand Down
10 changes: 2 additions & 8 deletions Assets.Scripts.Core/KeyHook.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using MOD.Scripts.Core;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -46,14 +47,7 @@ private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
int num = Marshal.ReadInt32(lParam);
if (num == 13 && GameSystem.Instance.HasFocus)
{
if (GameSystem.Instance.IsFullscreen)
{
GameSystem.Instance.DeFullscreen(PlayerPrefs.GetInt("width"), PlayerPrefs.GetInt("height"));
}
else
{
GameSystem.Instance.GoFullscreen();
}
MODWindowManager.FullscreenToggle(showToast: true);
return (IntPtr)1;
}
}
Expand Down
18 changes: 10 additions & 8 deletions Assets.Scripts.UI.Config/ScreenSwitcherButton.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Assets.Scripts.Core;
using MOD.Scripts.Core;
using UnityEngine;

namespace Assets.Scripts.UI.Config
Expand All @@ -7,6 +8,9 @@ public class ScreenSwitcherButton : MonoBehaviour
{
public int Width;

// This variable doesn't actually change/doesn't really relate to the game's fullscreen state
// It is actually used to indicate which button is labelled as "fullscreen" (it is true only for that button)

public bool IsFullscreen;

private UIButton button;
Expand All @@ -31,26 +35,24 @@ private int Height()

private void OnClick()
{
if (IsFullscreen)
if(IsFullscreen)
{
GameSystem.Instance.GoFullscreen();
MODWindowManager.FullscreenToggle(showToast: true);
}
else
{
int height = Height();
int width = Mathf.RoundToInt(height * GameSystem.Instance.AspectRatio);
GameSystem.Instance.DeFullscreen(width: width, height: height);
PlayerPrefs.SetInt("width", width);
PlayerPrefs.SetInt("height", height);
MODWindowManager.SetResolution(Height(), showToast: true);
}
}

private bool ShouldBeDown()
{
// Make the 'fullscreen' button always clickable because it toggles fullscreen
if (IsFullscreen)
{
return GameSystem.Instance.IsFullscreen;
return false;
}

return Screen.height == Height();
}

Expand Down
2 changes: 1 addition & 1 deletion Assets.Scripts.UI.Config/SwitchButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void Click()
}
break;
case ConfigButtonType.FullscreenMode:
GameSystem.Instance.GoFullscreen();
MODWindowManager.GoFullscreen(showToast: true);
break;
case ConfigButtonType.ClickToCutVoice:
GameSystem.Instance.StopVoiceOnClick = !GameSystem.Instance.StopVoiceOnClick;
Expand Down
Loading