-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from LittleBigRefresh/psp-setup
Automated setup of Allefresher
- Loading branch information
Showing
15 changed files
with
289 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Refresher.Verification; | ||
|
||
namespace Refresher.Patching; | ||
|
||
public interface IPatcher | ||
{ | ||
public List<Message> Verify(string url, bool patchDigest); | ||
|
||
public void Patch(string url, bool patchDigest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Refresher.Patching.PSP; | ||
|
||
public class PSPPluginListEntry | ||
{ | ||
public string Path; | ||
public int? Type; | ||
|
||
public PSPPluginListEntry(string path, int? type = null) | ||
{ | ||
this.Path = path; | ||
this.Type = type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace Refresher.Patching.PSP; | ||
|
||
public static class PSPPluginListParser | ||
{ | ||
public static List<PSPPluginListEntry> Parse(TextReader reader) | ||
{ | ||
List<PSPPluginListEntry> list = new(); | ||
|
||
while (reader.ReadLine() is { } line) | ||
{ | ||
//Skip blank lines | ||
if (string.IsNullOrWhiteSpace(line)) continue; | ||
|
||
string[] parts = line.Split(" "); | ||
|
||
PSPPluginListEntry entry = new(parts[0]); | ||
|
||
if (parts.Length > 1) | ||
{ | ||
entry.Type = int.Parse(parts[1]); | ||
} | ||
|
||
list.Add(entry); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
public static void Write(List<PSPPluginListEntry> list, TextWriter writer) | ||
{ | ||
foreach (PSPPluginListEntry entry in list) | ||
{ | ||
writer.Write(entry.Path); | ||
if (entry.Type.HasValue) | ||
{ | ||
writer.Write(' '); | ||
writer.Write(entry.Type.Value); | ||
} | ||
writer.Write('\n'); | ||
} | ||
|
||
writer.Flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Reflection; | ||
using Refresher.Patching.PSP; | ||
using Refresher.Verification; | ||
|
||
namespace Refresher.Patching; | ||
|
||
public class PSPPatcher : IPatcher | ||
{ | ||
public string? PSPDrivePath; | ||
|
||
private readonly Stream _allefresher; | ||
|
||
public PSPPatcher() | ||
{ | ||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
this._allefresher = assembly.GetManifestResourceStream("Refresher.Resources.Allefresher.prx")!; | ||
} | ||
|
||
public List<Message> Verify(string url, bool patchDigest) | ||
{ | ||
List<Message> messages = new(); | ||
|
||
if (!Uri.TryCreate(url, UriKind.Absolute, out _)) | ||
messages.Add(new Message(MessageLevel.Error, "URL failed to parse!")); | ||
|
||
if (string.IsNullOrEmpty(this.PSPDrivePath) || !Directory.Exists(this.PSPDrivePath)) | ||
messages.Add(new Message(MessageLevel.Error, "Invalid PSP Drive path!")); | ||
|
||
return messages; | ||
} | ||
|
||
public void Patch(string url, bool patchDigest) | ||
{ | ||
Uri uri = new(url); | ||
|
||
string domain = uri.Host; | ||
string format = $"{uri.Scheme}://%s:{uri.Port}{uri.AbsolutePath}%s"; | ||
|
||
string pluginsDir = Path.Combine(this.PSPDrivePath!, "SEPLUGINS"); | ||
|
||
//If the plugins directory does not exist | ||
if (!Directory.Exists(pluginsDir)) | ||
{ | ||
//Create it | ||
Directory.CreateDirectory(pluginsDir); | ||
} | ||
|
||
string domainPath = Path.Combine(pluginsDir, "Allefresher_domain.txt"); | ||
string formatPath = Path.Combine(pluginsDir, "Allefresher_format.txt"); | ||
|
||
//Delete the existing domain and format configuration files | ||
File.Delete(domainPath); | ||
File.Delete(formatPath); | ||
|
||
//Write the new domain and format configuration | ||
File.WriteAllText(domainPath, domain); | ||
File.WriteAllText(formatPath, format); | ||
|
||
//Match for all files called "game.txt" in the plugins directory | ||
//NOTE: we do this because the PSP filesystem is case insensitive, and the .NET STL is case sensitive on linux | ||
List<string> possibleMatches = Directory.EnumerateFiles(pluginsDir, "game.txt", new EnumerationOptions | ||
{ | ||
MatchCasing = MatchCasing.CaseInsensitive, | ||
}).ToList(); | ||
|
||
FileStream gamePluginsFileStream; | ||
|
||
const string allefresherPath = "ms0:/SEPLUGINS/Allefresher.prx"; | ||
|
||
List<PSPPluginListEntry> entries; | ||
if (possibleMatches.Any()) | ||
{ | ||
//Open the first match | ||
FileStream stream = File.OpenRead(possibleMatches[0]); | ||
|
||
//Read out the matches | ||
entries = PSPPluginListParser.Parse(new StreamReader(stream)); | ||
|
||
//If Allefresher is not in the list, | ||
if (!entries.Any(entry => entry.Path.Contains("Allefresher.prx", StringComparison.InvariantCultureIgnoreCase))) | ||
{ | ||
//Add Allefresher to the game plugin list | ||
entries.Add(new PSPPluginListEntry(allefresherPath, 1)); | ||
} | ||
|
||
//Dispose the read stream | ||
stream.Dispose(); | ||
|
||
//Open a new write stream to the game.txt file | ||
gamePluginsFileStream = File.Open(possibleMatches[0], FileMode.Truncate); | ||
} | ||
else | ||
{ | ||
//Create a new list, with the only entry being Allefresher | ||
entries = new List<PSPPluginListEntry> | ||
{ | ||
new(allefresherPath, 1), | ||
}; | ||
|
||
//Create a new game.txt file | ||
gamePluginsFileStream = File.Open(Path.Combine(pluginsDir, "game.txt"), FileMode.CreateNew); | ||
} | ||
|
||
//Write the plugin list to the file | ||
PSPPluginListParser.Write(entries, new StreamWriter(gamePluginsFileStream)); | ||
|
||
//Flush and dispose the stream | ||
gamePluginsFileStream.Flush(); | ||
gamePluginsFileStream.Dispose(); | ||
|
||
using FileStream allefresherOutput = File.Open(Path.Combine(pluginsDir, "Allefresher.prx"), FileMode.Create); | ||
|
||
this._allefresher.Seek(0, SeekOrigin.Begin); | ||
|
||
//Copy the Allefresher embedded resource to the output file | ||
this._allefresher.CopyTo(allefresherOutput); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.