-
Notifications
You must be signed in to change notification settings - Fork 300
feat: config support #562
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
Closed
Closed
feat: config support #562
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4300c97
feat: config support
Pendonym fb8f74e
Merge branch 'master' into master
Pendonym be81902
remove yaml conversion as its kinda weird and buggy and there is prob…
Pendonym d119bbb
i believe resolve everything
Pendonym b9be513
Merge branch 'master' into master
Pendonym 35ea53e
add rivals config support
Pendonym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,172 @@ | ||
| import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; | ||
| import CommonFormats from "src/CommonFormats.ts"; | ||
| import * as ini from 'ini'; | ||
|
|
||
| class configHandler implements FormatHandler { | ||
| public name: string = "config"; | ||
| public supportedFormats?: FileFormat[]; | ||
| public ready: boolean = false; | ||
|
|
||
| private readonly cfgExt = ['ini', 'cfg', 'cnf', 'conf', 'cf']; | ||
|
|
||
| async init() { | ||
| this.supportedFormats = [ | ||
| CommonFormats.JSON.builder("json").allowFrom(true).allowTo(true), | ||
| { | ||
| name: "INI Configuration File", | ||
| format: "ini", | ||
| extension: "ini", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "ini", | ||
| lossless: true, | ||
| }, | ||
| { | ||
| name: "Configuration File", | ||
| format: "cfg", | ||
| extension: "cfg", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "ini", | ||
| lossless: true, | ||
| }, | ||
| { | ||
| name: "Configuration File", | ||
| format: "cnf", | ||
| extension: "cnf", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "ini", | ||
| lossless: true, | ||
| }, | ||
| { | ||
| name: "Configuration File", | ||
| format: "conf", | ||
| extension: "conf", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "ini", | ||
| lossless: true, | ||
| }, | ||
| { | ||
| name: "Configuration File", | ||
| format: "cf", | ||
| extension: "cf", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "ini", | ||
| lossless: true, | ||
| }, | ||
| { | ||
| name: "RIVALS Configuration File", // https://www.roblox.com/games/17625359962 | ||
| format: "txt", | ||
| extension: "txt", | ||
| mime: "text/plain", | ||
| from: true, | ||
| to: true, | ||
| internal: "txt", | ||
| lossless: true, | ||
| }, | ||
| ]; | ||
| this.ready = true; | ||
| } | ||
|
|
||
| async doConvert( | ||
| inputFiles: FileData[], | ||
| inputFormat: FileFormat, | ||
| outputFormat: FileFormat, | ||
| ): Promise<FileData[]> { | ||
| const outputFiles: FileData[] = []; | ||
| const decoder = new TextDecoder(); | ||
| const encoder = new TextEncoder(); | ||
|
|
||
| const cfgRegex = new RegExp(`\\.(${this.cfgExt.join('|')})$`, 'i'); | ||
|
|
||
| // rivals -> json | ||
| if (inputFormat.name === "RIVALS Configuration File" && outputFormat.internal === "json") { | ||
| for (const file of inputFiles) { | ||
| const content = decoder.decode(file.bytes); | ||
| const decoded = content | ||
| .split("") | ||
| .reverse() | ||
| .map((char) => String.fromCharCode(char.charCodeAt(0) - 1)) | ||
| .join(""); | ||
|
|
||
| outputFiles.push({ | ||
| name: file.name.replace(/\.txt$/i, ".json"), | ||
| bytes: encoder.encode(decoded), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // json -> rivals | ||
| if (inputFormat.internal === "json" && outputFormat.name === "RIVALS Configuration File") { | ||
| for (const file of inputFiles) { | ||
| const content = decoder.decode(file.bytes); | ||
| const encoded = content | ||
| .split("") | ||
| .map((char) => String.fromCharCode(char.charCodeAt(0) + 1)) | ||
| .reverse() | ||
| .join(""); | ||
|
|
||
| outputFiles.push({ | ||
| name: file.name.replace(/\.json$/i, ".txt"), | ||
| bytes: encoder.encode(encoded), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // config -> json | ||
| if (inputFormat.internal === "ini" && outputFormat.internal === "json") { | ||
| for (const file of inputFiles) { | ||
| const decode = decoder.decode(file.bytes); | ||
| const parsed = ini.parse(decode); | ||
| const stringified = JSON.stringify(parsed, null, 2); | ||
|
|
||
| outputFiles.push({ | ||
| name: file.name.replace(cfgRegex, ".json"), | ||
| bytes: encoder.encode(stringified), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // json -> config | ||
| if (inputFormat.internal === "json" && outputFormat.internal === "ini") { | ||
| for (const file of inputFiles) { | ||
| const decode = decoder.decode(file.bytes); | ||
| const parsed = JSON.parse(decode); | ||
| const stringified = ini.stringify(parsed); | ||
|
|
||
| outputFiles.push({ | ||
| name: file.name.replace(/\.json$/i, `.${outputFormat.extension}`), | ||
| bytes: encoder.encode(stringified), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // config -> config | ||
| if (inputFormat.internal === outputFormat.internal) { | ||
| for (const file of inputFiles) { | ||
| outputFiles.push({ | ||
| name: file.name.replace(cfgRegex, `.${outputFormat.extension}`), | ||
| bytes: file.bytes, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (outputFiles.length === 0) { | ||
| throw new Error( | ||
| `configHandler does not support route: ${inputFormat.internal} -> ${outputFormat.internal}`, | ||
| ); | ||
| } | ||
|
Pendonym marked this conversation as resolved.
|
||
|
|
||
| return outputFiles; | ||
| } | ||
| } | ||
|
|
||
| export default configHandler; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did this come from??
A text configuration for a random Roblox game is incredibly out of scope for a general-purpose "config" handler, and for this pull request. There's also no distinction between this and a regular text file, given that all of
format,extension, andmimeare identical to a plaintext file.