Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep for plugin manager #123

Open
wants to merge 21 commits into
base: dev-branch
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
94 changes: 94 additions & 0 deletions TS SE Tool/CustomClasses/ExternalData/GameMod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2016-2020 LIPtoH <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System.IO;
using TS_SE_Tool.Utilities;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Linq;
using FuzzySharp;

namespace TS_SE_Tool {
public class GameMod {
public bool Enabled {
get => !File.IsDisabled();
set {
if (hasFile) File.Toggle(value);
}
}

[Browsable(false)]
public string Id { get; set; }
public string Name { get; set; } // { get => File?.FileNameWithoutExtension().Trim(); }
public DateTime? InstallDate {
get {
if (hasFile) return File.LastWriteTime;
return null;
}
}

[JsonIgnore]
public bool hasFile { get => File != null && File.Exists; }

[JsonIgnore]
[Browsable(false)]
public FileInfo File { get; set; }
[Browsable(false)]
public string FilePath { get => File?.FullName; }

public bool Delete() {
try {
if (hasFile) File.Delete();
} catch (Exception ex) {
IO_Utilities.LogWriter($"Failed to delete {File.FullString()}: {ex}");
return false;
}
return true;
}

public override bool Equals(object obj) {
if (obj == null || GetType() != obj.GetType())
return false;
var other = obj as GameMod;
return FilePath == other.FilePath;
}

public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + (FilePath ?? "").GetHashCode();
return hash;
}
}
}
public static class GameModExtensions {
public static IEnumerable<GameMod> AddSafe(this IEnumerable<GameMod> mods, GameMod pluginToAdd) {
List<GameMod> modList = new List<GameMod>(mods);
if (!modList.Any(plugin => plugin.FilePath == pluginToAdd.FilePath)) {
modList.Add(pluginToAdd);
}
return modList;
}
public static GameMod? GetDirectMatch(this IEnumerable<GameMod> mods, FileInfo file) {
return mods.FirstOrDefault(f => f.Name == file.FileNameWithoutExtension().Trim());
}
public static GameMod? GetFuzzyMatch(this IEnumerable<GameMod> mods, FileInfo file, int ratio = 70) {
return mods.FirstOrDefault(f => Fuzz.Ratio(file.Name, f.Name) > 70);
}
}
}
121 changes: 121 additions & 0 deletions TS SE Tool/CustomClasses/ExternalData/GamePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2016-2020 LIPtoH <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System.IO;
using TS_SE_Tool.Utilities;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Linq;
using FuzzySharp;

namespace TS_SE_Tool {
public class GamePlugin {
public bool Enabled {
get => !File32bit.IsDisabled() && !File64bit.IsDisabled();
set {
if (x86) File32bit.Toggle(value);
if (x64) File64bit.Toggle(value);
}
}
public string Name { get => File32bit?.FileNameWithoutExtension().RemoveAll("_win32", "_x86").Trim() ?? File64bit?.FileNameWithoutExtension().RemoveAll("_win64", "_x64").Trim(); }
public DateTime? InstallDate {
get {
if (x86) return File32bit.LastWriteTime;
else if (x64) return File64bit.LastWriteTime;
return null;
}
}

[JsonIgnore]
public bool x86 { get => File32bit != null && File32bit.Exists; }
[JsonIgnore]
public bool x64 { get => File64bit != null && File64bit.Exists; }

[JsonIgnore]
[Browsable(false)]
public FileInfo File32bit { get; set; }
[Browsable(false)]
public string Path32bit { get => File32bit?.FullName; }

[JsonIgnore]
[Browsable(false)]
public FileInfo File64bit { get; set; }
[Browsable(false)]
public string Path64bit { get => File64bit?.FullName; }

[JsonIgnore]
[Browsable(false)]
public List<FileInfo> Files {
get {
var list = new List<FileInfo>();
if (x86) list.Add(File32bit);
if (x64) list.Add(File64bit);
return list;
}
}

public bool Delete() {
try {
if (x86) File32bit.Delete();
} catch (Exception ex) {
IO_Utilities.LogWriter($"Failed to delete {File32bit.FullString()}: {ex}");
return false;
}
try {
if (x64) File64bit.Delete();
} catch (Exception ex) {
IO_Utilities.LogWriter($"Failed to delete {File64bit.FullString()}: {ex}");
return false;
}
return true;
}

public override bool Equals(object obj) {
if (obj == null || GetType() != obj.GetType())
return false;
GamePlugin other = (GamePlugin)obj;
return Path32bit == other.Path32bit && Path64bit == other.Path64bit;
}

public override int GetHashCode() {
// Combine hashes of Name, Path32bit, and Path64bit
unchecked {
int hash = 17;
//hash = hash * 23 + (Name ?? "").GetHashCode();
hash = hash * 23 + (Path32bit ?? "").GetHashCode();
hash = hash * 23 + (Path64bit ?? "").GetHashCode();
return hash;
}
}
}
public static class GamePluginExtensions {
public static IEnumerable<GamePlugin> AddSafe(this IEnumerable<GamePlugin> plugins, GamePlugin pluginToAdd) {
List<GamePlugin> pluginsList = new List<GamePlugin>(plugins);
if (!pluginsList.Any(plugin => plugin.Path32bit == pluginToAdd.Path32bit || plugin.Path64bit == pluginToAdd.Path64bit)) {
pluginsList.Add(pluginToAdd);
}
return pluginsList;
}
public static GamePlugin? GetDirectMatch(this IEnumerable<GamePlugin> plugins, FileInfo file) {
return plugins.FirstOrDefault(f => f.Name == file.FileNameWithoutExtension().RemoveAll("_win32", "_x86", "_win64", "_x64").Trim());
}
public static GamePlugin? GetFuzzyMatch(this IEnumerable<GamePlugin> plugins, FileInfo file, int ratio = 70) {
return plugins.FirstOrDefault(f => Fuzz.Ratio(file.Name, f.Name) > 70);
}
}
}
86 changes: 86 additions & 0 deletions TS SE Tool/CustomClasses/ExternalData/ProfileMod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2016-2020 LIPtoH <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System.IO;
using TS_SE_Tool.Utilities;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Linq;
using FuzzySharp;
using System.Windows.Forms;

namespace TS_SE_Tool {
public class ProfileMod {
[Browsable(false)]
public int LoadOrder { get; set; }
[AutoSizeColumnMode(DataGridViewAutoSizeColumnMode.ColumnHeader)]
[DisplayName("#")]
[JsonIgnore]
public int LoadOrderFriendly { get => LoadOrder + 1; }
[Browsable(false)]
public string Id { get; set; }
public string Name { get; set; } // { get => File?.FileNameWithoutExtension().Trim(); }

[JsonIgnore]
//[Browsable(false)]
[AutoSizeColumnMode(DataGridViewAutoSizeColumnMode.ColumnHeader)]
public bool Exists { get => GameMod != null && GameMod.File != null && GameMod.File.Exists; }

[Browsable(false)]
public GameMod? GameMod { get; set; }

public ProfileMod(string profileEntry, int loadOrder) {
var split = profileEntry.Trim('\"').Split('|');
Id = split.First();
Name = split.Last();
LoadOrder = loadOrder;
}

public bool Delete() {
return GameMod.Delete();
}

public override bool Equals(object obj) {
if (obj == null || GetType() != obj.GetType())
return false;
var other = obj as ProfileMod;
return Id == other.Id;
}

public override int GetHashCode() {
unchecked {
int hash = 17;
hash = hash * 23 + (Id ?? "").GetHashCode();
return hash;
}
}
}
public static class ProfileModExtensions {
public static IEnumerable<ProfileMod> AddSafe(this IEnumerable<ProfileMod> mods, ProfileMod modToAdd) {
List<ProfileMod> modList = new List<ProfileMod>(mods);
if (!modList.Any(mod => mod.Id == modToAdd.Id)) {
modList.Add(modToAdd);
}
return modList;
}
public static ProfileMod? GetDirectMatch(this IEnumerable<ProfileMod> mods, FileInfo file) {
return mods.FirstOrDefault(f => f.Name == file.FileNameWithoutExtension().Trim());
}
public static ProfileMod? GetFuzzyMatch(this IEnumerable<ProfileMod> mods, FileInfo file, int ratio = 70) {
return mods.FirstOrDefault(f => Fuzz.Ratio(file.Name, f.Name) > 70);
}
}
}
Loading