Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 551d41d

Browse files
committed
Add github config UI
1 parent bc74647 commit 551d41d

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

PluginLoader/Config/PluginConfig.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ public bool RemovePluginData(string id)
295295
return pluginSettings.Remove(id);
296296
}
297297

298+
public void SavePluginData(GitHubPluginConfig settings)
299+
{
300+
pluginSettings[settings.Id] = settings;
301+
}
298302

299303
public void AddDevelopmentFolder(string folder)
300304
{

PluginLoader/Data/GitHubPlugin.cs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using avaness.PluginLoader.Compiler;
22
using avaness.PluginLoader.Config;
3+
using avaness.PluginLoader.GUI;
34
using avaness.PluginLoader.Network;
45
using ProtoBuf;
56
using Sandbox.Graphics.GUI;
@@ -61,6 +62,8 @@ public override bool LoadData(ref PluginDataConfig config, bool enabled)
6162
return true;
6263
}
6364

65+
this.config = null;
66+
6467
if (config != null)
6568
{
6669
config = null;
@@ -132,7 +135,7 @@ public override Assembly GetAssembly()
132135

133136
string dllFile = Path.Combine(cacheDir, pluginFile);
134137
string commitFile = Path.Combine(cacheDir, commitHashFile);
135-
string selectedCommit = GetSelectedCommit();
138+
string selectedCommit = GetSelectedVersion()?.Commit ?? Commit;
136139
if (!File.Exists(dllFile) || !File.Exists(commitFile) || File.ReadAllText(commitFile) != selectedCommit || Main.Instance.Config.GameVersionChanged)
137140
{
138141
var lbl = Main.Instance.Splash;
@@ -153,14 +156,11 @@ public override Assembly GetAssembly()
153156
return a;
154157
}
155158

156-
private string GetSelectedCommit()
159+
private Branch GetSelectedVersion()
157160
{
158-
if (config == null || string.IsNullOrWhiteSpace(config.SelectedVersion) || AlternateVersions == null)
159-
return Commit;
160-
Branch branch = AlternateVersions.FirstOrDefault(x => x.Name.Equals(config.SelectedVersion, StringComparison.OrdinalIgnoreCase));
161-
if (branch == null)
162-
return Commit;
163-
return branch.Commit;
161+
if (config == null || string.IsNullOrWhiteSpace(config.SelectedVersion))
162+
return null;
163+
return AlternateVersions?.FirstOrDefault(x => x.Name.Equals(config.SelectedVersion, StringComparison.OrdinalIgnoreCase));
164164
}
165165

166166
public byte[] CompileFromSource(string commit, Action<float> callback = null)
@@ -236,6 +236,61 @@ public override void InvalidateCache()
236236
catch { }
237237
}
238238

239+
public override void AddDetailControls(PluginDetailMenu screen, MyGuiControlBase bottomControl, out MyGuiControlBase topControl)
240+
{
241+
if(AlternateVersions == null || AlternateVersions.Length == 0)
242+
{
243+
topControl = null;
244+
return;
245+
}
246+
247+
string selectedCommit = GetSelectedVersion()?.Commit ?? Commit;
248+
MyGuiControlCombobox versionDropdown = new MyGuiControlCombobox();
249+
versionDropdown.AddItem(-1, "Default");
250+
int selectedKey = -1;
251+
for (int i = 0; i < AlternateVersions.Length; i++)
252+
{
253+
Branch version = AlternateVersions[i];
254+
versionDropdown.AddItem(i, version.Name);
255+
if (version.Commit == selectedCommit)
256+
selectedKey = i;
257+
}
258+
versionDropdown.SelectItemByKey(selectedKey);
259+
versionDropdown.ItemSelected += () =>
260+
{
261+
PluginConfig mainConfig = Main.Instance.Config;
262+
263+
int selectedKey = (int)versionDropdown.GetSelectedKey();
264+
string newVersion = selectedKey >= 0 ? AlternateVersions[selectedKey].Name : null;
265+
string currentVersion = GetSelectedVersion()?.Name;
266+
if (currentVersion == newVersion)
267+
return;
268+
269+
if (config == null)
270+
{
271+
config = new GitHubPluginConfig()
272+
{
273+
Id = Id,
274+
};
275+
276+
mainConfig.SavePluginData(config);
277+
}
278+
279+
config.SelectedVersion = newVersion;
280+
mainConfig.Save();
281+
if(mainConfig.IsEnabled(Id))
282+
screen.InvokeOnRestartRequired();
283+
};
284+
285+
screen.PositionAbove(bottomControl, versionDropdown, MyAlignH.Left);
286+
screen.Controls.Add(versionDropdown);
287+
288+
MyGuiControlLabel lblVersion = new MyGuiControlLabel(text: "Installed Version");
289+
screen.PositionAbove(versionDropdown, lblVersion, align: MyAlignH.Left, spacing: 0);
290+
screen.Controls.Add(lblVersion);
291+
topControl = lblVersion;
292+
}
293+
239294
[ProtoContract]
240295
public class Branch
241296
{

0 commit comments

Comments
 (0)