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

Commit

Permalink
Created abstract class for downloading oblivion mods
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 28, 2019
1 parent 91ca518 commit 83b3059
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 60 deletions.
87 changes: 87 additions & 0 deletions OMODFramework.Test/DownloadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
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/>.
*/

using System;
using System.IO;
using System.Net;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pathoschild.FluentNexus;
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;

namespace OMODFramework.Test
{
public abstract class DownloadTest
{
private string _apiKey;
private NexusClient _client;

public abstract string DownloadFileName { get; set; }
public abstract string FileName { get; set; }
public abstract int ModID { get; set; }
public abstract int FileID { get; set; }

[TestInitialize]
protected void Setup()
{
Framework.TempDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestTempDir");

if (File.Exists(DownloadFileName) && File.Exists(FileName))
return;

if(!File.Exists("nexus_api_key.txt"))
throw new Exception("Nexus API Key file does not exist!");

_apiKey = File.ReadAllText("nexus_api_key.txt");

_client = new NexusClient(_apiKey, "OMODFramework Unit Tests", "0.0.1");

var limits = _client.GetRateLimits().Result;

if(limits.IsBlocked() && !File.Exists(DownloadFileName))
throw new Exception("Rate limit blocks all Nexus Connections!");

var downloadLinks = _client.ModFiles.GetDownloadLinks("oblivion", ModID, FileID).Result;

using (var client = new WebClient())
{
client.DownloadFile(downloadLinks[0].Uri, DownloadFileName);
}

if(File.Exists(FileName))
return;

using (var zipStream = new ZipFile(File.OpenRead(DownloadFileName)))
using (var fs = new FileStream(FileName, FileMode.CreateNew))
{
foreach (ZipEntry ze in zipStream)
{
if(ze.IsFile && ze.Name.ToLower().Contains("omod"))
zipStream.GetInputStream(ze).CopyTo(fs);
}
}
}

[TestCleanup]
protected void Cleanup()
{
Framework.CleanTempDir(true);
}
}
}
65 changes: 5 additions & 60 deletions OMODFramework.Test/OMODTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,23 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pathoschild.FluentNexus;
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;

namespace OMODFramework.Test
{
[TestClass]
public class OMODTests
public class OMODTests : DownloadTest
{
private string _apiKey;
private NexusClient _client;

// testing with DarNified UI from
// https://www.nexusmods.com/oblivion/mods/10763
private const string DownloadFileName = "DarNified UI 1.3.2.zip";
private const string FileName = "DarNified UI 1.3.2.omod";
private const int ModID = 10763;
private const int FileID = 34631;

[TestInitialize]
public void Setup()
{
Framework.TempDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestTempDir");

if (File.Exists(DownloadFileName) && File.Exists(FileName))
return;

if(!File.Exists("nexus_api_key.txt"))
throw new Exception("Nexus API Key file does not exist!");

_apiKey = File.ReadAllText("nexus_api_key.txt");

_client = new NexusClient(_apiKey, "OMODFramework Unit Tests", "0.0.1");

var limits = _client.GetRateLimits().Result;

if(limits.IsBlocked() && !File.Exists(DownloadFileName))
throw new Exception("Rate limit blocks all Nexus Connections!");

var downloadLinks = _client.ModFiles.GetDownloadLinks("oblivion", ModID, FileID).Result;

using (var client = new WebClient())
{
client.DownloadFile(downloadLinks[0].Uri, DownloadFileName);
}

if(File.Exists(FileName))
return;

using (var zipStream = new ZipFile(File.OpenRead(DownloadFileName)))
using (var fs = new FileStream(FileName, FileMode.CreateNew))
{
foreach (ZipEntry ze in zipStream)
{
if(ze.IsFile && ze.Name.ToLower().Contains("omod"))
zipStream.GetInputStream(ze).CopyTo(fs);
}
}
}
public override string DownloadFileName { get; set; } = "DarNified UI 1.3.2.zip";
public override string FileName { get; set; } = "DarNified UI 1.3.2.omod";
public override int ModID { get; set; } = 10763;
public override int FileID { get; set; } = 34631;

[TestMethod]
public void TestOMOD()
Expand Down Expand Up @@ -177,11 +128,5 @@ public void TestCreation()
Assert.IsTrue(contents == text1 || contents == text2 || contents == text3);
});
}

[TestCleanup]
public void Cleanup()
{
Framework.CleanTempDir(true);
}
}
}
130 changes: 130 additions & 0 deletions OMODFramework.Test/ScriptFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
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/>.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OMODFramework.Scripting;

namespace OMODFramework.Test
{
internal class ScriptFunctions : IScriptFunctions
{
public void Warn(string msg)
{
Debug.WriteLine($"[Warning]: {msg}");
}

public void Message(string msg)
{
Debug.WriteLine($"[Message]: {msg}");
}

public void Message(string msg, string title)
{
Debug.WriteLine($"[Message]: {title} - {msg}");
}

public List<int> Select(List<string> items, string title, bool isMultiSelect, List<string> previews, List<string> descriptions)
{
Debug.WriteLine($"[Select]: {title} multi: {isMultiSelect}");
for (var i = 0; i < items.Count; i++)
{
var preview = previews?[i] != null ? previews[i] : "empty";
var description = descriptions?[i] != null ? descriptions[i] : "empty";
Debug.Write($"Items: {items[i]} | preview: {preview} | description: {description}");
}
return new List<int>{0};
}

public string InputString(string title, string initialText, bool useRTF)
{
Debug.WriteLine($"[InputString]: {title}, text: {initialText}, rtf: {useRTF}");
return "Hello World";
}

public int DialogYesNo(string title)
{
Debug.WriteLine($"[DialogYesNo]: {title}");
return 1;
}

public int DialogYesNo(string title, string message)
{
Debug.WriteLine($"[DialogYesNo]: {title} - {message}");
return 1;
}

public void DisplayImage(string path, string title)
{
Debug.WriteLine($"[Image]: {title} - {path}");
}

public void DisplayText(string text, string title)
{
Debug.WriteLine($"[Text]: {title} - {text}");
}

public void Patch(string from, string to)
{
Debug.WriteLine($"[Patch]: {from} - {to}");
}

public string ReadOblivionINI(string section, string name)
{
Debug.WriteLine($"[ReadOblivionINI]: {section} - {name}");
return "";
}

public string ReadRendererInfo(string name)
{
Debug.WriteLine($"[ReadRendererInfo]: {name}");
return "";
}

public bool DataFileExists(string path)
{
Debug.WriteLine($"[DataFileExists]: {path}");
return false;
}

public bool HasScriptExtender()
{
return true;
}

public bool HasGraphicsExtender()
{
return false;
}

public Version ScriptExtenderVersion()
{
return new Version(0, 0, 21, 4);
}

public Version GraphicsExtenderVersion()
{
return new Version("");
}

public Version OblivionVersion()
{
return new Version(1, 2, 0, 416);
}
}
}
37 changes: 37 additions & 0 deletions OMODFramework.Test/ScriptTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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/>.
*/

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OMODFramework.Test
{
public class ScriptTest : DownloadTest
{
// testing with NoMaaM Breathing Idles from
// https://www.nexusmods.com/oblivion/mods/40462
public override string DownloadFileName { get; set; } = "NoMaaM Breathing Idles V1 OMOD-40462-1-0.omod";
public override string FileName { get; set; } = "NoMaaM Breathing Idles V1 OMOD-40462-1-0.omod";
public override int ModID { get; set; } = 10763;
public override int FileID { get; set; } = 85415;

[TestMethod]
public void TestOBMMScript()
{
var omod = new OMOD(FileName);
}
}
}

0 comments on commit 83b3059

Please sign in to comment.