-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb6cdf6
commit 1206b9b
Showing
41 changed files
with
727 additions
and
128 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,52 @@ | ||
import { IResolver } from '@pssbletrngle/pack-resolver' | ||
import { Acceptor, IResolver } from '@pssbletrngle/pack-resolver' | ||
import { Id } from '../common/id.js' | ||
import { fromJson } from '../textHelper.js' | ||
import Registry from '../common/registry.js' | ||
import { RegistryProvider } from '../emit/index.js' | ||
import { Logger } from '../logger.js' | ||
|
||
export type AcceptorWithLoader = (logger: Logger, ...paramenters: Parameters<Acceptor>) => ReturnType<Acceptor> | ||
|
||
export default interface Loader { | ||
loadFrom(resolver: IResolver): Promise<void> | ||
} | ||
|
||
export abstract class JsonLoader<T> implements RegistryProvider<T> { | ||
protected readonly registry = new Registry<T>() | ||
|
||
protected abstract parse(logger: Logger, json: unknown, id: Id): T | null | ||
|
||
forEach(consumer: (recipe: T, id: Id) => void): void { | ||
this.registry.forEach(consumer) | ||
} | ||
|
||
private tryParseJson(logger: Logger, content: string) { | ||
try { | ||
return fromJson(content) | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
logger.warn(`unable to parse json: ${error.message}`, content) | ||
return null | ||
} | ||
throw error | ||
} | ||
} | ||
|
||
readonly accept: AcceptorWithLoader = (logger, path, content) => { | ||
const match = /data\/(?<namespace>[\w-]+)\/\w+\/(?<rest>[\w-/]+).json/.exec(path) | ||
if (!match?.groups) return false | ||
|
||
const { namespace, rest } = match.groups | ||
const id: Id = { namespace, path: rest } | ||
|
||
const json = this.tryParseJson(logger.group(path), content.toString()) | ||
if (!json) return false | ||
|
||
const parsed = this.parse(logger, json, id) | ||
if (!parsed) return false | ||
|
||
this.registry.set(id, parsed) | ||
|
||
return true | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,13 @@ | ||
import { Logger } from '../logger.js' | ||
import { Acceptor } from '@pssbletrngle/pack-resolver' | ||
import { encodeId, Id } from '../common/id.js' | ||
import { fromJson } from '../textHelper.js' | ||
import Registry from '../common/registry.js' | ||
import { tryCatching } from '../error.js' | ||
import { RegistryProvider } from '../emit/index.js' | ||
import { LootTable, LootTableSchema } from '../schema/loot.js' | ||
import { JsonLoader } from './index.js' | ||
import { Logger } from '../logger.js' | ||
|
||
export default class LootTableLoader implements RegistryProvider<LootTable> { | ||
private readonly registry = new Registry<LootTable>() | ||
|
||
constructor(private readonly logger: Logger) {} | ||
|
||
private parse(raw: unknown, id: Id): LootTable | null { | ||
const logger = this.logger.group(`error parsing loot table ${encodeId(id)}`) | ||
return tryCatching(logger, () => { | ||
return LootTableSchema.parse(raw) | ||
export default class LootTableLoader extends JsonLoader<LootTable> { | ||
protected parse(logger: Logger, json: unknown, id: Id): LootTable | null { | ||
return tryCatching(logger.group(`error parsing loot table ${encodeId(id)}`), () => { | ||
return LootTableSchema.parse(json) | ||
}) | ||
} | ||
|
||
readonly accept: Acceptor = (path, content) => { | ||
// TODO extract common logic | ||
const match = /data\/(?<namespace>[\w-]+)\/loot_tables\/(?<rest>[\w-/]+).json/.exec(path) | ||
if (!match?.groups) return false | ||
|
||
const { namespace, rest } = match.groups | ||
const id: Id = { namespace, path: rest } | ||
|
||
const json = fromJson(content.toString()) | ||
|
||
const parsed = this.parse(json, id) | ||
if (!parsed) return false | ||
|
||
this.registry.set(id, parsed) | ||
|
||
return true | ||
} | ||
|
||
forEach(consumer: (recipe: LootTable, id: Id) => void): void { | ||
this.registry.forEach(consumer) | ||
} | ||
} |
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.