-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutoLoadGame.cs
More file actions
153 lines (141 loc) · 5.05 KB
/
AutoLoadGame.cs
File metadata and controls
153 lines (141 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2016 Allis Tauri
using System.IO;
using UnityEngine;
using SaveUpgradePipeline;
namespace AutoLoadGame
{
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class AutoLoadGame : MonoBehaviour
{
private static bool loaded;
private static AutoLoadGame instance;
private static string savesDir;
private static string config;
public static void Log(string msg, params object[] args) =>
Debug.Log(string.Format("[AutoLoadGame]: " + msg, args));
private string game = "";
private string save = "";
private void LoadGame()
{
//load the game only the first time (i.e. at game start)
if(loaded)
return;
loaded = true;
//get the game and the save
if(File.Exists(config))
{
var cfg = ConfigNode.Load(config);
if(cfg != null)
{
var val = cfg.GetValue("game");
if(val != null)
game = val;
val = cfg.GetValue("save");
if(val != null)
save = val;
}
else
{
Log("Configuration file is empty: {0}", config);
return;
}
}
else
{
Log("Configuration file not found: {0}", config);
return;
}
var gameDir = Path.Combine(savesDir, game);
if(!Directory.Exists(gameDir))
{
Log("No game directory: {0}", gameDir);
return;
}
var saveFile = Path.Combine(gameDir, save + ".sfs");
if(!File.Exists(saveFile))
{
Log("No such file: {0}", saveFile);
return;
}
//load the game
var game_node = GamePersistence.LoadSFSFile(save, game);
if(game_node == null)
{
Log("Unable to load the save: {0}", saveFile);
return;
}
Log("Loading: {0}/{1}", game, save);
KSPUpgradePipeline.Process(game_node,
game,
LoadContext.SFS,
OnLoadDialogPipelineFinished,
(opt, n) => Log("KSPUpgradePipeline finished with error: {0}", saveFile));
}
void OnLoadDialogPipelineFinished(ConfigNode node)
{
HighLogic.CurrentGame = GamePersistence.LoadGameCfg(node, game, true, false);
if(HighLogic.CurrentGame != null)
{
if(GamePersistence.UpdateScenarioModules(HighLogic.CurrentGame))
{
if(node != null)
GameEvents.onGameStatePostLoad.Fire(node);
GamePersistence.SaveGame(HighLogic.CurrentGame, save, game, SaveMode.OVERWRITE);
}
if(HighLogic.CurrentGame.startScene == GameScenes.FLIGHT)
{
AutoSwitchVessel.activeVessel = HighLogic.CurrentGame.flightState.activeVesselIdx;
AutoSwitchVessel.save = save;
}
HighLogic.CurrentGame.startScene = GameScenes.SPACECENTER;
HighLogic.SaveFolder = game;
HighLogic.CurrentGame.Start();
}
}
private void onLevelWasLoaded(GameScenes scene)
{
if(scene != GameScenes.MAINMENU)
return;
Log("MAINMENU is loaded. Waiting 60 frames and loading the save.");
StartCoroutine(CallbackUtil.DelayedCallback(60, LoadGame));
}
private void Awake()
{
if(instance != null)
{
Destroy(this);
return;
}
GameEvents.onLevelWasLoadedGUIReady.Add(onLevelWasLoaded);
DontDestroyOnLoad(this);
instance = this;
savesDir = Path.Combine(KSPUtil.ApplicationRootPath, "saves");
config = Path.Combine(savesDir, "AutoLoadGame.conf");
}
}
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class AutoSwitchVessel : MonoBehaviour
{
public static string save = "persistent";
public static int activeVessel = -1;
private static void switch_to_active_vessel()
{
FlightDriver.StartAndFocusVessel(save, activeVessel);
activeVessel = -1;
}
private void onLevelWasLoaded(GameScenes scene)
{
if(scene != GameScenes.SPACECENTER || activeVessel < 0)
return;
AutoLoadGame.Log("SPACECENTER is loaded. Waiting 60 frames and switching to the active vessel.");
StartCoroutine(CallbackUtil.DelayedCallback(60, switch_to_active_vessel));
}
private void Awake()
{
GameEvents.onLevelWasLoadedGUIReady.Add(onLevelWasLoaded);
}
}
}