Firstborn/Assets/Advanced INI Parser/INIReader.cs

239 lines
13 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using RPGCreationKit;
using UnityEngine.UI;
using System.Globalization;
public class INIReader : MonoBehaviour
{
[Header("File path")]
[SerializeField] private string iniPath;
[Header("Objects that need initialized on load")]
[SerializeField] private Slider ScreenshotRes;
[SerializeField] private Slider MusicVolume;
[SerializeField] private Slider UIVolume;
[SerializeField] private List<Toggle> ScreenshotExtension;
[SerializeField] private bool InitializeSettings = false;
// Start is called before the first frame update
void Start() {
// Set all data if none exists
iniPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\";
if (Directory.Exists(iniPath) == false){
Directory.CreateDirectory(iniPath);
}
if (!File.Exists(iniPath+@"\Settings.ini")) {
// Create a file to write to.
using (StreamWriter sw = File.CreateText(iniPath+@"\Settings.ini")) {
sw.WriteLine("[General]");
sw.WriteLine("NPC_TO_NPC_CONVERSATION_DISTANCE = 3.5");
sw.WriteLine("NPC_LOOKAT_TARGET_DISTANCE = 5");
sw.WriteLine("NPC_STOP_FOLLOW_AFTER_DISTANCE = 2.2");
sw.WriteLine("PLAYER_HEARS_NPC_DIALOGUES_DISTANCE = 15");
sw.WriteLine("PLAYER_DISTANCE_TO_DIALOGUE = 3");
sw.WriteLine("PLAYER_DISTANCE_TO_DISPLAY_ENEMY_BAR = 100");
sw.WriteLine("CHARGED_ATTACK_MOUSE_DOWN_TIME = 0.5");
sw.WriteLine("MIN_VELOCITY_TO_CONTINUE_ROTATING_ARROW = 5");
sw.WriteLine("DISTANCE_SHOW_NPC_IN_COMBAT_UI = 100");
sw.WriteLine("MELEE_GENERAL_RIGIDBODY_FORCE = 20");
sw.WriteLine("ARROW_GENERAL_RIGIDBODY_FORCE = 5");
sw.WriteLine("MELEE_ONBODY_RIGIDBODY_FORCE = 35");
sw.WriteLine("ARROW_ONBODY_RIGIDBODY_FORCE = 10");
sw.WriteLine("DRAIN_HEALTH_DEFAULT_RATE = 200");
sw.WriteLine("DRAIN_STAMINA_DEFAULT_RATE = 200");
sw.WriteLine("DISTANCE_WHEN_NPCS_START_LOOK_AT = 3.5");
sw.WriteLine("CROSSHAIR_HIT_TIME = .25");
sw.WriteLine("STOPPING_DETECTION_THRESHOLD = 0.15");
sw.WriteLine("HOSTILE_FACTION_COMBAT_THERSHOLD = 10");
sw.WriteLine("[EDITOR]");
sw.WriteLine("EDITOR_AI_LOAD_LOCATION = Assets/RPG Creation Kit/Prefab Library/AI/");
sw.WriteLine("EDITOR_AI_SAVE_LOCATION = Assets/RPG Creation Kit/Prefab Library/AI/");
sw.WriteLine("EDITOR_CREATUREAI_LOAD_LOCATION = Assets/RPG Creation Kit/Prefab Library/AI/Creatures/");
sw.WriteLine("EDITOR_CREATUREAI_SAVE_LOCATION = Assets/RPG Creation Kit/Prefab Library/AI/Creatures/");
sw.WriteLine("[Code]");
sw.WriteLine("DEFAULT_WEAPON_REACH = 4.0");
sw.WriteLine("INTERACTOR_RAYCAST_MAXDISTANCE = ");
sw.WriteLine("DRAIN_STAMINA_ON_ATTACK_SPEEDAMOUNT = 25");
sw.WriteLine("DRAIN_STAMINA_ON_ATTACKBLOCKED_SPEEDAMOUNT = 25");
sw.WriteLine("DRAIN_MANA_ON_CAST_SPEEDAMOUNT = 25");
sw.WriteLine("PROJECTILE_DESPAWN_TIME = 30");
sw.WriteLine("DISTANCE_BEFORE_ROTATING_TO_TARGET = 4.5");
sw.WriteLine("[Save Game]");
sw.WriteLine("JSON_PRETTY_PRINT = false");
sw.WriteLine("PLAYER_DAMAGE_SPEED = 100");
sw.WriteLine("[NEW GAME START]");
sw.WriteLine("RCK_NEW_STARTING_LOCATION = Starting cave");
sw.WriteLine("RCK_NEW_STARTING_WORLDSPACEID = 00CanyonInteriors");
sw.WriteLine("RCK_NEW_STARTING_CELLID = CanyonCaveCell1-1Cave2");
sw.WriteLine("RCK_NEW_STARTING_LEVEL = 1");
sw.WriteLine("STARTING_POSX = 93.52182");
sw.WriteLine("STARTING_POSX = -6.028");
sw.WriteLine("STARTING_POSX = -76.271");
sw.WriteLine("STARTING_ROTX = 0");
sw.WriteLine("STARTING_ROTX = 0");
sw.WriteLine("STARTING_ROTX = 0");
sw.WriteLine("ATTRIBUTES_DEF_HEALTH = 200.0");
sw.WriteLine("ATTRIBUTES_DEF_STAMINA = 200.0");
sw.WriteLine("ATTRIBUTES_DEF_MANA = 200.0");
sw.WriteLine("[Screenshot Tool]");
sw.WriteLine("Screenshot = 0");
sw.WriteLine("ScreenshotMultiplier = 2");
sw.WriteLine("ScreenshotExtension = 1");
sw.WriteLine("[Sound Settings]");
sw.WriteLine("MusicVolume = 1");
sw.WriteLine("UIVolume = 1");
}
} else {
if (InitializeSettings) {
// General Settings
RCKSettings.NPC_TO_NPC_CONVERSATION_DISTANCE = float.Parse(ReadSettingString("General", "NPC_TO_NPC_CONVERSATION_DISTANCE", "3.5"));
RCKSettings.NPC_LOOKAT_TARGET_DISTANCE = float.Parse(ReadSettingString("General", "NPC_LOOKAT_TARGET_DISTANCE", "5"));
RCKSettings.NPC_STOP_FOLLOW_AFTER_DISTANCE = float.Parse(ReadSettingString("General", "NPC_STOP_FOLLOW_AFTER_DISTANCE", "2.2"));
RCKSettings.PLAYER_HEARS_NPC_DIALOGUES_DISTANCE = float.Parse(ReadSettingString("General", "PLAYER_HEARS_NPC_DIALOGUES_DISTANCE", "15"));
RCKSettings.PLAYER_DISTANCE_TO_DIALOGUE = float.Parse(ReadSettingString("General", "PLAYER_DISTANCE_TO_DIALOGUE", "3"));
RCKSettings.PLAYER_DISTANCE_TO_DISPLAY_ENEMY_BAR = float.Parse(ReadSettingString("General", "PLAYER_DISTANCE_TO_DISPLAY_ENEMY_BAR", "100"));
RCKSettings.CHARGED_ATTACK_MOUSE_DOWN_TIME = float.Parse(ReadSettingString("General", "CHARGED_ATTACK_MOUSE_DOWN_TIME", "0.5"));
RCKSettings.MIN_VELOCITY_TO_CONTINUE_ROTATING_ARROW = float.Parse(ReadSettingString("General", "MIN_VELOCITY_TO_CONTINUE_ROTATING_ARROW", "5"));
RCKSettings.DISTANCE_SHOW_NPC_IN_COMBAT_UI = float.Parse(ReadSettingString("General", "DISTANCE_SHOW_NPC_IN_COMBAT_UI", "100"));
RCKSettings.MELEE_GENERAL_RIGIDBODY_FORCE = float.Parse(ReadSettingString("General", "MELEE_GENERAL_RIGIDBODY_FORCE", "20"));
RCKSettings.ARROW_GENERAL_RIGIDBODY_FORCE = float.Parse(ReadSettingString("General", "ARROW_GENERAL_RIGIDBODY_FORCE", "5"));
RCKSettings.MELEE_ONBODY_RIGIDBODY_FORCE = float.Parse(ReadSettingString("General", "MELEE_ONBODY_RIGIDBODY_FORCE", "35"));
RCKSettings.ARROW_ONBODY_RIGIDBODY_FORCE = float.Parse(ReadSettingString("General", "ARROW_ONBODY_RIGIDBODY_FORCE", "10"));
RCKSettings.DRAIN_HEALTH_DEFAULT_RATE = float.Parse(ReadSettingString("General", "DRAIN_HEALTH_DEFAULT_RATE", "200"));
RCKSettings.DRAIN_STAMINA_DEFAULT_RATE = float.Parse(ReadSettingString("General", "DRAIN_STAMINA_DEFAULT_RATE", "200"));
RCKSettings.DISTANCE_WHEN_NPCS_START_LOOK_AT = float.Parse(ReadSettingString("General", "DISTANCE_WHEN_NPCS_START_LOOK_AT", "3.5"));
RCKSettings.CROSSHAIR_HIT_TIME = float.Parse(ReadSettingString("General", "CROSSHAIR_HIT_TIME", ".25"));
RCKSettings.STOPPING_DETECTION_THRESHOLD = float.Parse(ReadSettingString("General", "STOPPING_DETECTION_THRESHOLD", "0.15"));
RCKSettings.HOSTILE_FACTION_COMBAT_THERSHOLD = float.Parse(ReadSettingString("General", "HOSTILE_FACTION_COMBAT_THERSHOLD", "10"));
// EDITOR
RCKSettings.EDITOR_AI_LOAD_LOCATION = ReadSettingString("EDITOR", "EDITOR_AI_LOAD_LOCATION", "Assets/RPG Creation Kit/Prefab Library/AI/");
RCKSettings.EDITOR_AI_SAVE_LOCATION = ReadSettingString("EDITOR", "EDITOR_AI_SAVE_LOCATION", "Assets/RPG Creation Kit/Prefab Library/AI/");
RCKSettings.EDITOR_CREATUREAI_LOAD_LOCATION = ReadSettingString("EDITOR", "EDITOR_CREATUREAI_LOAD_LOCATION", "Assets/RPG Creation Kit/Prefab Library/AI/Creatures/");
RCKSettings.EDITOR_CREATUREAI_SAVE_LOCATION = ReadSettingString("EDITOR", "EDITOR_CREATUREAI_SAVE_LOCATION", "Assets/RPG Creation Kit/Prefab Library/AI/Creatures/");
//Code
RCKSettings.DEFAULT_WEAPON_REACH = float.Parse(ReadSettingString("General", "DEFAULT_WEAPON_REACH", "4.0"));
float TempA = float.Parse(ReadSettingString("General", "INTERACTOR_RAYCAST_MAXDISTANCE", "0"));
if (TempA == 0f) {
RCKSettings.INTERACTOR_RAYCAST_MAXDISTANCE = float.MaxValue;
} else {
RCKSettings.INTERACTOR_RAYCAST_MAXDISTANCE = TempA;
}
RCKSettings.DRAIN_STAMINA_ON_ATTACK_SPEEDAMOUNT = float.Parse(ReadSettingString("General", "DRAIN_STAMINA_ON_ATTACK_SPEEDAMOUNT", "25"));
RCKSettings.DRAIN_STAMINA_ON_ATTACKBLOCKED_SPEEDAMOUNT = float.Parse(ReadSettingString("General", "DRAIN_STAMINA_ON_ATTACKBLOCKED_SPEEDAMOUNT", "25"));
RCKSettings.DRAIN_MANA_ON_CAST_SPEEDAMOUNT = float.Parse(ReadSettingString("General", "DRAIN_MANA_ON_CAST_SPEEDAMOUNT", "25"));
RCKSettings.PROJECTILE_DESPAWN_TIME = float.Parse(ReadSettingString("General", "PROJECTILE_DESPAWN_TIME", "30"));
RCKSettings.DISTANCE_BEFORE_ROTATING_TO_TARGET = float.Parse(ReadSettingString("General", "DISTANCE_BEFORE_ROTATING_TO_TARGET", "4.5"));
// Save Game
RCKSettings.JSON_PRETTY_PRINT = ReadSettingBool("Save Game", "JSON_PRETTY_PRINT", false);
RCKSettings.PLAYER_DAMAGE_SPEED = float.Parse(ReadSettingString("Save Game", "PLAYER_DAMAGE_SPEED", "100"));
// NEW GAME START
RCKSettings.RCK_NEW_STARTING_LOCATION = ReadSettingString("NEW GAME START", "RCK_NEW_STARTING_LOCATION", "Starting cave");
RCKSettings.RCK_NEW_STARTING_WORLDSPACEID = ReadSettingString("NEW GAME START", "RCK_NEW_STARTING_WORLDSPACEID", "00CanyonInteriors");
RCKSettings.RCK_NEW_STARTING_CELLID = ReadSettingString("NEW GAME START", "RCK_NEW_STARTING_CELLID", "CanyonCaveCell1-1Cave2");
RCKSettings.RCK_NEW_STARTING_LEVEL = ReadSettingInt("NEW GAME START", "RCK_NEW_STARTING_LEVEL", 1);
float StartPOSX = float.Parse(ReadSettingString("NEW GAME START", "STARTING_POSX", "93.52182"));
float StartPOSY = float.Parse(ReadSettingString("NEW GAME START", "STARTING_POSY", "-6.028"));
float StartPOSZ = float.Parse(ReadSettingString("NEW GAME START", "STARTING_POSZ", "-76.271"));
RCKSettings.RCK_NEW_STARTING_POS = new Vector3(StartPOSX, StartPOSY, StartPOSZ);
float StartROTX = float.Parse(ReadSettingString("NEW GAME START", "STARTING_ROTX", "0"));
float StartROTY = float.Parse(ReadSettingString("NEW GAME START", "STARTING_ROTY", "0"));
float StartROTZ = float.Parse(ReadSettingString("NEW GAME START", "STARTING_ROTZ", "0"));
RCKSettings.RCK_NEW_STARTING_ROT = new Vector3(StartROTX, StartROTY, StartROTZ);
RCKSettings.ATTRIBUTES_DEF_HEALTH = float.Parse(ReadSettingString("NEW GAME START", "ATTRIBUTES_DEF_HEALTH", "200.0"));
RCKSettings.ATTRIBUTES_DEF_STAMINA = float.Parse(ReadSettingString("NEW GAME START", "ATTRIBUTES_DEF_STAMINA", "200.0"));
RCKSettings.ATTRIBUTES_DEF_MANA = float.Parse(ReadSettingString("NEW GAME START", "ATTRIBUTES_DEF_MANA", "200.0"));
// Screenshot tools
ScreenshotRes.value = (float)ReadSettingInt("Screenshot Tool", "ScreenshotMultiplier", 0);
int A = ReadSettingInt("Screenshot Tool", "ScreenshotExtension", 0);
ScreenshotExtension[A].isOn = true;
}
// Sound settings
MusicVolume.value = float.Parse(ReadSettingString("Sound Settings", "MusicVolume", "1"));
UIVolume.value = float.Parse(ReadSettingString("Sound Settings", "UIVolume", "1"));
}
}
public void SetScreenShotExtension(int A) {
SaveSettingInt("Screenshot Tool", "ScreenshotExtension", A);
}
public void SetMusicVolume() {
SaveSettingString("Sound Settings", "MusicVolume", MusicVolume.value.ToString());
}
public void SetUIVolume() {
SaveSettingString("Sound Settings", "UIVolume", UIVolume.value.ToString());
}
/////////////////////////////////////////
////// - Read and save settings - ///////
/////////////////////////////////////////
public int ReadSettingInt(string CategoryName, string SettingName, int CurrentValue = 0){
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
int Value = ini.ReadValue(CategoryName, SettingName, CurrentValue);
ini.Close();
return Value;
}
public void SaveSettingInt(string CategoryName, string SettingName, int NewValue) {
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
ini.WriteValue(CategoryName, SettingName, NewValue);
ini.Close();
}
public string ReadSettingString(string CategoryName, string SettingName, string CurrentValue){
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
string Value = ini.ReadValue(CategoryName, SettingName, CurrentValue);
ini.Close();
return Value;
}
public void SaveSettingString(string CategoryName, string SettingName, string NewValue) {
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
ini.WriteValue(CategoryName, SettingName, NewValue);
ini.Close();
}
public bool ReadSettingBool(string CategoryName, string SettingName, bool CurrentValue){
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
bool Value = ini.ReadValue(CategoryName, SettingName, CurrentValue);
ini.Close();
return Value;
}
public void SaveSettingBool(string CategoryName, string SettingName, bool NewValue) {
INIParser ini = new INIParser();
ini.Open(iniPath+@"\Settings.ini");
ini.WriteValue(CategoryName, SettingName, NewValue);
ini.Close();
}
public void Debugger(string A) {
}
}