Skip to content

Commit

Permalink
Reworking projectData
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanV2 committed Aug 1, 2024
1 parent e10b146 commit 41bcc07
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 243 deletions.
3 changes: 1 addition & 2 deletions src/Lib/Internal/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export namespace Json {
if (typeof doc === "object") {
file = doc.uri;
content = doc.getText();
}
else {
} else {
content = doc;
}

Expand Down
40 changes: 19 additions & 21 deletions src/Lib/Internal/Types/Manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PackType } from '../../Project/PackType';
import { TextDocument } from '../../Types/TextDocument';
import { Json } from '../Json';
import { PackType } from "../../Project/PackType";
import { TextDocument } from "../../Types/TextDocument";
import { Json } from "../Json";

/**
*
Expand Down Expand Up @@ -73,13 +73,12 @@ export interface ManifestMetadata {
license?: string;
url?: string;
generated_with?: {
[tool_name: string]: string[]
}
[tool_name: string]: string[];
};
}

/** */
export namespace Manifest {

export function is(value: any): value is Manifest {
if (typeof value === "object") {
if (typeof value.format_version !== "number") return false;
Expand All @@ -96,7 +95,7 @@ export namespace Manifest {
* @param m
* @returns
*/
export function IsWorldManifest(m: Manifest): boolean {
export function isWorldManifest(m: Manifest): boolean {
const modules = m.modules;
if (modules === undefined) return false;

Expand All @@ -114,7 +113,7 @@ export namespace Manifest {
* @param m
* @returns
*/
export function IsResourceManifest(m: Manifest): boolean {
export function isResourceManifest(m: Manifest): boolean {
const modules = m.modules;
if (modules === undefined) return false;

Expand All @@ -132,7 +131,7 @@ export namespace Manifest {
* @param m
* @returns
*/
export function IsBehaviorManifest(m: Manifest): boolean {
export function isBehaviorManifest(m: Manifest): boolean {
const modules = m.modules;
if (modules === undefined) return false;

Expand All @@ -150,7 +149,7 @@ export namespace Manifest {
* @param m
* @returns
*/
export function IsSkinpackManifest(m: Manifest): boolean {
export function isSkinpackManifest(m: Manifest): boolean {
const modules = m.modules;
if (modules === undefined) return false;

Expand All @@ -168,7 +167,7 @@ export namespace Manifest {
* @param m
* @returns
*/
export function DetectType(m: Manifest): PackType {
export function detectType(m: Manifest): PackType {
if (!m.modules) return PackType.unknown;

for (let I = 0; I < m.modules.length; I++) {
Expand Down Expand Up @@ -197,30 +196,29 @@ export namespace Manifest {
* @param uri
* @returns
*/
export function GetManifest(uri: string, getDocument: (uri: string) => TextDocument | undefined): Manifest | undefined {
export function getManifest(
uri: string,
getDocument: (uri: string) => TextDocument | undefined
): Manifest | undefined {
const doc = getDocument(uri);

if (doc) return Json.To<Manifest>(doc);

return undefined;
}

export function DetectTypeUri(manifestUri: string, getDocument: (uri: string) => TextDocument | undefined): PackType {
const Type = PackType.detect(manifestUri);
export function detectTypeUri(manifestUri: string, manifest: Manifest): PackType {
const type = PackType.detect(manifestUri);

switch (Type) {
switch (type) {
case PackType.behavior_pack:
case PackType.resource_pack:
case PackType.skin_pack:
case PackType.world:
return Type;
return type;

case PackType.unknown:
default:
const manifest = Manifest.GetManifest(manifestUri, getDocument);
if (!manifest) break;

const SubType = Manifest.DetectType(manifest);
const SubType = Manifest.detectType(manifest);
return SubType;
}

Expand Down
25 changes: 10 additions & 15 deletions src/Lib/Project/BehaviorPack/BehaviorPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ import { Pack } from "../../Types/Pack";
import { TextDocument } from "../../Types/TextDocument";
import { FileType } from "./FileType";
import { PackType } from "../PackType";
import { Manifest } from "../../Internal/Types";

type CollectFieldsOfType<T> = {
[K in keyof T]: T[K] extends DataSet<infer U> ? U : never;
};
type CollectionFieldsDataSet<T> = {
[K in keyof T]: T[K] extends DataSet<infer U> ? DataSet<U> : never;
}
};

type ItemTypes = CollectFieldsOfType<BehaviorPack>[keyof BehaviorPack];
type ItemTypes = CollectFieldsOfType<BehaviorPack>[keyof BehaviorPack];
type DataSetTypes = CollectionFieldsDataSet<BehaviorPack>[keyof BehaviorPack];

/** */
export class BehaviorPack implements Container, Pack {
/**@inheritdoc */
readonly type: PackType = PackType.behavior_pack;

/**The folder path of the pack*/
readonly folder: string;
/**The context of the project*/
readonly context: MCProject;
readonly manifest: Manifest;

/**The collection of animations*/
readonly animations: DataSet<Animation.Animation>;
Expand All @@ -61,11 +59,11 @@ export class BehaviorPack implements Container, Pack {

/**
* @param folder The folder of the behavior
* @param Context The Mcproject data or the filepath to read from.*/
constructor(folder: string, Context: MCProject | string) {
* @param context The Mcproject data or the filepath to read from.*/
constructor(folder: string, context: MCProject | string, manifest: Manifest) {
this.folder = folder;
this.context =
typeof Context === "object" ? Context : MCProject.loadSync(Context);
this.manifest = manifest;
this.context = typeof context === "object" ? context : MCProject.loadSync(context);

this.animations = new DataSet();
this.animation_controllers = new DataSet();
Expand Down Expand Up @@ -117,8 +115,7 @@ export class BehaviorPack implements Container, Pack {
return this.trading.set(Trading.Process(doc));

case FileType.feature:
return this.features.set(Feature.Process(doc))

return this.features.set(Feature.Process(doc));
}

return undefined;
Expand Down Expand Up @@ -211,9 +208,7 @@ export class BehaviorPack implements Container, Pack {
* @param predicate
* @returns
*/
find(
predicate: (value: ItemTypes, key: string) => boolean
): ItemTypes | undefined {
find(predicate: (value: ItemTypes, key: string) => boolean): ItemTypes | undefined {
let value = undefined;

if ((value = this.animations.find(predicate))) return value;
Expand Down
8 changes: 5 additions & 3 deletions src/Lib/Project/BehaviorPack/BehaviorPackCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Item from "./Item";
import * as LootTable from "./LootTable";
import * as Structure from "./Structure";
import * as Trading from "./Trading";
import { Manifest } from "../../Internal/Types";

/** */
export class BehaviorPackCollection extends PackCollection<BehaviorPack> {
Expand Down Expand Up @@ -51,11 +52,12 @@ export class BehaviorPackCollection extends PackCollection<BehaviorPack> {
/**
*
* @param folder
* @param Context
* @param context
* @param manifest
* @returns
*/
add(folder: string, Context: MCProject | string): BehaviorPack {
const out = new BehaviorPack(folder, Context);
add(folder: string, context: MCProject | string, manifest: Manifest): BehaviorPack {
const out = new BehaviorPack(folder, context, manifest);
this.packs.push(out);

return out;
Expand Down
Loading

0 comments on commit 41bcc07

Please sign in to comment.