Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions FrameworkMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ namespace PlanetbaseFramework
{
public class FrameworkMod : ModBase
{
public const string AssemblyVersion = "2.2.2.0";
public const string AssemblyVersion = "2.3.2.0";
public new static readonly Version ModVersion = new Version(AssemblyVersion);
public const string DefaultModName = "Planetbase Framework";

public FrameworkMod()
{
Utils.ErrorTexture = ModTextures.Find(x => x.name.Equals("error.png"));
}

public override string ModName { get; } = "Planetbase Framework";
public override string ModName { get; } = DefaultModName;

public override void Init()
{
Expand All @@ -24,7 +25,7 @@ public override void Init()

private class ModTitleButton : TitleButton
{
public ModTitleButton() : base("mod_titlemenu") { }
public ModTitleButton() : base("fmod_titlemenu") { }

public override void HandleAction(GameStateTitle gst)
{
Expand Down
33 changes: 33 additions & 0 deletions GuiRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using UnityEngine;
using Planetbase;

namespace PlanetbaseFramework
{
class GuiRenderer : Planetbase.GuiRenderer
{

public bool renderBigButton(Rect rect, string text, FontSize fontSize, GUIStyle style, SoundDefinition sound = null)
{
if (style == null)
{
float aspect = rect.width / rect.height;
style = (aspect <= 1.5f) ? Singleton<GuiStyles>.getInstance().getBigButtonStyle(fontSize, aspect) : Singleton<GuiStyles>.getInstance().getBigTextButtonStyle(fontSize, aspect);
}
if (!GUI.Button(rect, text, style))
{
return false;
}
Singleton<AudioPlayer>.getInstance().play((sound != null) ? sound : SoundListMenu.getInstance().ButtonClick, null);
return true;
}

public bool renderTitleButton(Rect rect, string text, FontSize fontSize, GUIStyle style, bool playSound = false)
{
return this.renderBigButton(rect, text, fontSize, style, null);
}

}
}


90 changes: 90 additions & 0 deletions IModConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

namespace PlanetbaseFramework
{

public interface IModConfig
{

bool isActive(string modName);
void setActive(string modName, bool state);

}

public class SimpleFileBasedModConfig : IModConfig
{

private string configFile;
private Dictionary<string, string> modConfigMap = null;

public SimpleFileBasedModConfig(string configFile)
{
this.configFile = configFile;
}

private void loadConfig()
{
modConfigMap = new Dictionary<string, string>();
if (!System.IO.File.Exists(configFile))
{
Debug.Log("Config file {0} not found. empty config..");
return;
}
System.IO.StreamReader reader = new System.IO.StreamReader(configFile);
String line = null;
while ((line = reader.ReadLine()) != null)
{
Regex commentRegex = new Regex(@"^#.*");
Regex propertyRegex = new Regex(@".+=.*");
if (commentRegex.Match(line).Success || !propertyRegex.Match(line).Success)
continue;

String[] parts = line.Split(new char[] { '=' }, 2);
modConfigMap.Add(parts[0], (parts.Length > 1) ? parts[1] : "");
}
reader.Close();
}

private void saveConfig()
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(configFile, false);

foreach (KeyValuePair<string, string> entry in modConfigMap)
{
writer.WriteLine("{0}={1}", entry.Key, entry.Value);
}
writer.Flush();
writer.Close();
}

public bool isActive(string modName)
{
if (modConfigMap == null)
loadConfig();
return (modConfigMap.ContainsKey(modName) && modConfigMap[modName].Equals("1"));
}

public void setActive(string modName, bool state)
{
if (modConfigMap == null)
loadConfig();

if (state)
{
if (!modConfigMap.ContainsKey(modName))
modConfigMap.Add(modName, "1");
}
else
{
if (modConfigMap.ContainsKey(modName))
modConfigMap.Remove(modName);
}
saveConfig();
}

}

}
72 changes: 72 additions & 0 deletions IModMetaData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace PlanetbaseFramework
{


public interface IModMetaData
{

string GetName();
Version GetVersion();
string GetRootPath();
ModBase GetMod();
List<Texture2D> GetTextures();
List<GameObject> GetObjects();

// TODO: dependencies

}

public class SelfModMetaData : IModMetaData
{

private ModBase mod;

public SelfModMetaData(ModBase mod)
{
this.mod = mod;
}

public ModBase GetMod()
{
return mod;
}

public string GetName()
{
return mod.ModName;
}

public Version GetVersion()
{
return mod.ModVersion;
}

public string GetRootPath()
{
return mod.ModPath;
}

public List<Texture2D> GetTextures()
{
return mod.ModTextures;
}

public List<GameObject> GetObjects()
{
return mod.ModObjects;
}


}

/*
// TODO: implement xml based meta data
public class XmlModMetaData {

}
*/
}
5 changes: 4 additions & 1 deletion ModBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class ModBase
public List<Texture2D> ModTextures { get; protected set; }
public List<GameObject> ModObjects { get; protected set; }


public virtual Version ModVersion => new Version(0, 0, 0, 0);

private HarmonyInstance Harmony { get; set; }
Expand Down Expand Up @@ -100,7 +101,7 @@ protected ModBase()
{
ModObjects = LoadAllObjs("obj");

if(ModObjects.Count > 0)
if (ModObjects.Count > 0)
{
Debug.Log($"Successfully loaded {ModObjects.Count} object(s)");
}
Expand Down Expand Up @@ -130,6 +131,8 @@ public virtual void Update()
{
}

public virtual void Cleanup() { }

public int LoadAllStrings(string subfolder = null)
{
var files = GetAssetsMatchingFileType("xml", subfolder);
Expand Down
45 changes: 38 additions & 7 deletions ModListGameState.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Planetbase;
using UnityEngine;
using System.Collections.Generic;

namespace PlanetbaseFramework
{
//This class was thrown together fairly quickly, but should be a decent example on making new gamestates.
//qp: followed instructions above..
public class ModListGameState : GameState
{
private GuiRenderer Renderer { get; } = new GuiRenderer();
Expand All @@ -20,10 +22,16 @@ public override void onGui()
return;
}

List<IModMetaData> datas = ModManager.getInstance().GetMetaDatas();
PrintLine("Loaded Mods:", 0);
for(var i = 0; i < ModLoader.ModList.Count; i++)
for (var i = 0; i < datas.Count; i++)
{
PrintLine(ModLoader.ModList[i].ModName, i + 1);
string name = datas[i].GetName();
string text = string.Format("{0} - ({1})", name, datas[i].GetVersion().ToString());
if (name.Equals(Utils.GetFrameworkMod().ModName))
PrintLine(text, i + 1, (ModManager.getInstance().isModActive(name)) ? Color.green : Color.red, FontSize.Normal);
else
PrintLine(text, i + 1, (ModManager.getInstance().isModActive(name)) ? Color.green : Color.gray, FontSize.Normal, name);
}

if (Renderer.renderBackButton(
Expand All @@ -39,13 +47,36 @@ public override void onGui()

private void PrintLine(string text, int lineNumber)
{
Vector2 textLocation = new Vector2(50, 80);
GUIStyle labelStyle = Renderer.getLabelStyle(FontSize.Huge, FontStyle.Bold, TextAnchor.LowerLeft, FontType.Title);
labelStyle.normal.textColor = Color.blue;
PrintLine(text, lineNumber, Color.blue);
}

GUI.Label(new Rect(textLocation.x, textLocation.y + (GuiRenderer.getMenuButtonSize(FontSize.Huge).y )* lineNumber, Screen.width, GuiRenderer.getMenuButtonSize(FontSize.Huge).y - 30), text, labelStyle);
private void PrintLine(string text, int lineNumber, Color color, FontSize size = FontSize.Huge, string modName = null)
{
float deltaY = (GuiRenderer.getMenuButtonSize(FontSize.Huge).y);

labelStyle.normal.textColor = Color.white;
if (modName != null)
{
Vector2 startLocation = new Vector2(50, 200);
Rect rect = new Rect(startLocation.x, startLocation.y + deltaY * lineNumber, GuiRenderer.getMenuButtonSize(FontSize.Huge).x - 30, GuiRenderer.getMenuButtonSize(FontSize.Huge).y - 30);
float aspect = rect.width / rect.height;
GUIStyle btnStyle = Singleton<GuiStyles>.getInstance().getBigTextButtonStyle(FontSize.Normal, aspect);
Color c = btnStyle.normal.textColor;
btnStyle.normal.textColor = color;
if (Renderer.renderTitleButton(rect, text, FontSize.Normal))
{
ModManager.getInstance().setModActive(modName, !(color == Color.green));
}
btnStyle.normal.textColor = c;
}
else
{
Vector2 startLocation = new Vector2(55, 200);
GUIStyle labelStyle = Renderer.getLabelStyle(size, FontStyle.Bold, TextAnchor.LowerLeft, FontType.Title);
labelStyle.normal.textColor = color;
GUI.Label(new Rect(startLocation.x, startLocation.y + deltaY * lineNumber, Screen.width, GuiRenderer.getMenuButtonSize(FontSize.Huge).y - 30), text, labelStyle);

labelStyle.normal.textColor = Color.white;
}
}
}
}
Loading