Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.

Commit

Permalink
Implemented INI and Renderer read functions
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 28, 2019
1 parent 4c068c9 commit 69d8381
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 2 deletions.
120 changes: 120 additions & 0 deletions OMODFramework/Classes/Oblivion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright (C) 2019 erri120
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
* This file contains parts of the Oblivion Mod Manager licensed under GPLv2
* and has been modified for use in this OMODFramework
* Original source: https://www.nexusmods.com/oblivion/mods/2097
* GPLv2: https://opensource.org/licenses/gpl-2.0.php
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace OMODFramework.Classes
{
internal static class OblivionINI
{
internal static string GetINIValue(string section, string name)
{
var result = "";
GetINISection(section, out var list);
if(list == null)
throw new OMODFrameworkException($"Oblivion.ini section {section} does not exist!");

list.Where(s => s.Trim().ToLower().StartsWith($"{name.ToLower()}=")).Do(s =>
{
var res = s.Substring(s.IndexOf('=') + 1).Trim();
var i = res.IndexOf(';');
if (i != -1)
res = res.Substring(0, i - 1);
result = res;
});

return result;
}

internal static void GetINISection(string section, out List<string> list)
{
if(string.IsNullOrWhiteSpace(Framework.OblivionINIFile))
throw new OMODFrameworkException("OblivionINI.GetINISection requires a path to oblivion.ini");

var contents = new List<string>();
var inSection = false;
using (var sr = new StreamReader(File.OpenRead(Framework.OblivionINIFile), Encoding.UTF8))
{
try
{
while (sr.Peek() != -1)
{
var s = sr.ReadLine();
if (s == null)
break;
if (inSection)
{
if (s.Trim().StartsWith("[") && s.Trim().EndsWith("]")) break;
contents.Add(s);
}
else
{
if (s.Trim().ToLower() == section)
inSection = true;
}
}
}
catch (Exception e)
{
throw new OMODFrameworkException($"Could not read from oblivion.ini at {Framework.OblivionINIFile}\n{e}");
}
}

if (!inSection) list = null;
list = contents;
}
}

internal static class OblivionRenderInfo
{
internal static string GetInfo(string s)
{
if(string.IsNullOrWhiteSpace(Framework.OblivionRenderInfoFile))
throw new OMODFrameworkException("OblivionRenderInfo.GetInfo requires a path to the RenderInfo.txt file");

var result = $"Value {s} not found";

try
{
var lines = File.ReadAllLines(Framework.OblivionRenderInfoFile);
lines.Where(t => t.Trim().ToLower().StartsWith(s)).Do(t =>
{
var split = t.Split(':');
if (split.Length != 2) result = "-1";
result = split[1].Trim();
});
}
catch (Exception e)
{
throw new OMODFrameworkException($"Could not read from RenderInfo.txt file at {Framework.OblivionRenderInfoFile}\n{e}");
}

return result;
}
}
}
32 changes: 32 additions & 0 deletions OMODFramework/Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public class Framework

internal static string OblivionDataFolder => Path.Combine(OblivionGameFolder, "data");

/// <summary>
/// Absolute path to the oblivion.ini file
/// </summary>
public static string OblivionINIFile { get; set; } = "";

/// <summary>
/// Absolute path to the RendererInfo.txt file
/// </summary>
public static string OblivionRenderInfoFile { get; set; } = "";

/// <summary>
/// Methods for patching files.
/// <para><c>OverwriteGameFolder</c> - will overwrite the file from the oblivion data folder if found</para>
Expand All @@ -55,6 +65,28 @@ public enum PatchMethod { OverwriteGameFolder, CreatePatchGameFolder, CreatePatc
/// </summary>
public static PatchMethod CurrentPatchMethod { get; set; } = PatchMethod.CreatePatchInMod;

/// <summary>
/// Methods for reading the oblivion.ini file
/// <para><c>ReadOriginalINI</c> - reads the oblivion.ini specified at <see cref="OblivionINIFile"/></para>
/// <para><c>ReadWithInterface</c> - will call <c>IScriptFunctions.ReadOblivionINI</c></para>
/// </summary>
public enum ReadINIMethod { ReadOriginalINI, ReadWithInterface }
/// <summary>
/// Method used for reading the oblivion.ini file, see <see cref="ReadINIMethod"/> for all available options
/// </summary>
public static ReadINIMethod CurrentReadINIMethod { get; set; } = ReadINIMethod.ReadOriginalINI;
/// <summary>
/// Methods for reading the RendererInfo.txt file
/// <para><c>ReadOriginalRenderer</c> - reads the RendererInfo.txt specified at <see cref="OblivionRenderInfoFile"/></para>
/// <para><c>ReadWithInterface</c> - will call <c>IScriptFunctions.ReadRendererInfo</c></para>
/// </summary>
public enum ReadRendererMethod { ReadOriginalRenderer, ReadWithInterface }
/// <summary>
/// Method used for reading the RendererInfo.txt file, see <see cref="ReadRendererMethod"/> for all available options
/// </summary>
public static ReadRendererMethod CurrentReadRendererMethod { get; set; } =
ReadRendererMethod.ReadOriginalRenderer;

public static bool EnableWarnings { get; set; } = false;

public static void CleanTempDir(bool deleteRoot = false)
Expand Down
15 changes: 15 additions & 0 deletions OMODFramework/Scripting/IScriptFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,20 @@ List<int> Select(
/// <param name="from">Absolute path to the file from the OMOD</param>
/// <param name="to">Relative path to the file inside the data folder which may or may not exist</param>
void Patch(string from, string to);

/// <summary>
/// Read the oblivion.ini file and return the value of a field using its key and section name
/// </summary>
/// <param name="section"></param>
/// <param name="name"></param>
/// <returns></returns>
string ReadOblivionINI(string section, string name);

/// <summary>
/// Reads the RendererInfo.txt file and returns the value of the field using its name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string ReadRendererInfo(string name);
}
}
54 changes: 52 additions & 2 deletions OMODFramework/Scripting/OBMMScriptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You should have received a copy of the GNU General Public License
using System.IO;
using System.Linq;
using System.Text;
using OMODFramework.Classes;
using Directory = Alphaleonis.Win32.Filesystem.Directory;
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;
Expand Down Expand Up @@ -540,10 +541,10 @@ internal static ScriptReturnData Execute(string inputScript, string dataPath, st
FunctionInputString(line);
break;
case "ReadINI":
//TODO: FunctionReadINI(line);
FunctionReadINI(line);
break;
case "ReadRendererInfo":
//TODO: FunctionReadRenderer(line);
FunctionReadRenderer(line);
break;
case "ExecLines":
FunctionExecLines(line, ref extraLines);
Expand Down Expand Up @@ -2335,5 +2336,54 @@ private static void FunctionRegisterBSA(IReadOnlyCollection<string> line, bool r
else
srd.RegisterBSASet.RemoveWhere(s => s == esp);
}

private static void FunctionReadINI(IReadOnlyCollection<string> line)
{
if (line.Count < 4)
{
Warn("Missing arguments for 'ReadINI'");
return;
}

if(line.Count > 4) Warn("Unexpected extra arguments for 'ReadINI'");

switch (Framework.CurrentReadINIMethod)
{
case Framework.ReadINIMethod.ReadOriginalINI:
variables[line.ElementAt(1)] = OblivionINI.GetINIValue(line.ElementAt(2), line.ElementAt(3));
break;
case Framework.ReadINIMethod.ReadWithInterface:
var s = _scriptFunctions.ReadOblivionINI(line.ElementAt(2), line.ElementAt(3));
variables[line.ElementAt(1)] = s ?? throw new OMODFrameworkException("Could not read the oblivion.ini file using the function IScriptFunctions.ReadOblivionINI");
break;
default:
throw new OMODFrameworkException("Unknown ReadINIMethod for Framework.CurrentReadINIMethod!");
}

}

private static void FunctionReadRenderer(IReadOnlyCollection<string> line)
{
if (line.Count < 3)
{
Warn("Missing arguments for 'ReadRendererInfo'");
return;
}

if(line.Count > 3) Warn("Unexpected extra arguments for 'ReadRendererInfo'");

switch (Framework.CurrentReadRendererMethod)
{
case Framework.ReadRendererMethod.ReadOriginalRenderer:
variables[line.ElementAt(1)] = OblivionRenderInfo.GetInfo(line.ElementAt(2));
break;
case Framework.ReadRendererMethod.ReadWithInterface:
var s = _scriptFunctions.ReadRendererInfo(line.ElementAt(2));
variables[line.ElementAt(1)] = s ?? throw new OMODFrameworkException("Could not read the RenderInfo.txt file using the function IScriptFunctions.ReadRendererInfo");
break;
default:
throw new OMODFrameworkException("Unknown ReadRendererMethod for Framework.CurrentReadRendererMethod!");
}
}
}
}

0 comments on commit 69d8381

Please sign in to comment.