Added comments

This commit is contained in:
Robert Altner 2019-08-01 12:30:07 +02:00
parent ee8b68b957
commit 9eb12c8207

View file

@ -9,13 +9,21 @@ namespace Mod
{
public class Main : MonoBehaviour
{
// Logs a message made by this mod
// [CROSSHAIRMOD]Message
private void Log(string message)
{
System.Console.WriteLine("[CROSSHAIRMOD]" + message);
}
// Initialize Settings dictionary
private Dictionary<string, int> m_settings = new Dictionary<string, int>();
// Initialize Crosshair texture
private Texture2D crosshair = new Texture2D(-1, -1);
// Initialize State checker. If this is false, the crosshair won't be drawn
// This is a temporary fix to stop this mod from spamming errors in the log
private bool m_validState = true;
// Reads settings file and sets all variables
@ -23,39 +31,47 @@ namespace Mod
{
Log("Accessing Settings at " + filepath);
// Try to read file contents into string
string settings = "";
try
{
settings = System.IO.File.ReadAllText(filepath);
} catch(Exception e)
{
// Log error and return invalid state
Log(e.Message);
return false;
}
sett_dict = new Dictionary<string, int>();
// Empty settings dictionary
sett_dict.Clear();
// Splice settings string by newline characters, to get each line into a new array member
string[] lines = settings.Split('\n');
foreach (string line in lines)
foreach (string line in lines) // Loop through all lines
{
if (line == "")
if (line == "") // If line is empty, ignore it (some editors add newlines when saving, this will combat that)
continue;
string[] vals = line.Split('=');
string[] vals = line.Split('='); // Split each line by the = character, to get a key and a value
sett_dict.Add(vals[0], Int32.Parse(vals[1]));
sett_dict.Add(vals[0], Int32.Parse(vals[1])); // Store key and value in settings dictionary
}
Log("Successfully loaded settings!");
return true;
return true; // Return valid state
}
// Creates a crosshair texture
private bool createCrossTexture(ref Texture2D texture)
{
// Assign dictionary values to variables
int m_crosshairLength = 0, m_crosshairThickness = 0, m_crosshairColorRed = 0, m_crosshairColorGreen = 0, m_crosshairColorBlue = 0, m_crosshairColorAlpha = 0;
if(!m_settings.TryGetValue("crosshairLength", out m_crosshairLength))
// Checks wether the key is in the dictionary, and assigns it
// If the function finds that a necessary setting is missing, it will
// return an invalid state
if (!m_settings.TryGetValue("crosshairLength", out m_crosshairLength))
{
Log("Missing Setting: crosshairLength");
return false;
@ -91,12 +107,13 @@ namespace Mod
return false;
}
// Construct color object from RGBA values
Color m_crosshairColor = new Color(m_crosshairColorRed / 255f,
m_crosshairColorGreen / 255f,
m_crosshairColorBlue / 255f,
m_crosshairColorAlpha / 255f);
// Lazy
// Lazily add 1 to thickness if it's even. If it is even the crosshair can't be symmetrical
m_crosshairThickness += (m_crosshairThickness % 2 == 0) ? 1 : 0;
// Create Texture with approprate dimensions. The m_crosshairLength is the distance
@ -128,28 +145,36 @@ namespace Mod
}
}
// Set texture settings and apply changes
texture.wrapMode = TextureWrapMode.Repeat;
texture.Apply();
return true;
return true; // Return valid state
}
// This will be executed first
void Start()
{
// Read settings, if this fails, the state will be invalid
m_validState = readSettings(".\\Blackwake_Data\\Managed\\Mods\\chSettings.sett", ref m_settings);
foreach (KeyValuePair<string, int> setting in m_settings)
Log(setting.Key + ": " + setting.Value);
}
void OnGUI()
{
// If the mod is in an invalid state, the OnGUI function won't execute
if (m_validState)
{
// If the state is valid, construct crosshair texture, and update state
// TODO: Remove createCrossTexture, this doesn't need to be constructed each time
m_validState = createCrossTexture(ref crosshair);
// Create GUIStyle to render the crosshair
GUIStyle style = new GUIStyle();
style.normal.background = crosshair;
// Render the crosshair
GUI.Label(new Rect(Screen.width / 2 - crosshair.width / 2, Screen.height / 2 - crosshair.height / 2, crosshair.width, crosshair.height),
crosshair, style);
}