Skip to content

Commit

Permalink
Update Zirconium, fix history, fix auto focus, fix context logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorlias committed Dec 30, 2021
1 parent d95e756 commit 6be55f7
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 61 deletions.
10 changes: 7 additions & 3 deletions example/server/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ ZirconServer.Registry.Init(
.CreateDefaultUserGroup()
.CreateDefaultAdminGroup()
.AddFunction(
new ZirconFunctionBuilder("ping").Bind((context) => {
context.LogInfo("Pong!");
new ZirconFunctionBuilder("ping").AddArgument("string?").Bind((context, response) => {
if (response !== undefined) {
context.LogInfo("Pong! with {Argument}", response);
} else {
context.LogInfo("Pong!");
}
}),
["User"],
)
Expand All @@ -78,7 +82,7 @@ ZirconServer.Registry.Init(
)
.AddFunction(ZirconPrint)
.AddFunction(
new ZirconFunctionBuilder("with_varadic")
new ZirconFunctionBuilder("with_variadic")
.AddArgument("string")
.AddArgument("player")
.AddArgument("number?")
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"@rbxts/snapdragon": "2.0.0-beta.1",
"@rbxts/string-utils": "^1.0.3",
"@rbxts/t": "^1.3.5",
"@rbxts/zirconium": "^1.1.0-beta.1"
"@rbxts/zirconium": "^1.1.0-beta.2"
}
}
2 changes: 2 additions & 0 deletions src/Class/StatefulZirconValidator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ZrValue } from "@rbxts/zirconium/out/Data/Locals";
import ZrUndefined from "@rbxts/zirconium/out/Data/Undefined";
import { ZirconValidator } from "./ZirconTypeValidator";

export abstract class StatefulZirconValidator<T, U = never> implements ZirconValidator<T, U> {
Expand Down
12 changes: 4 additions & 8 deletions src/Class/ZirconContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DestructureMode, TemplateTokenKind } from "@rbxts/message-templates/out
import ZrContext from "@rbxts/zirconium/out/Data/Context";
import { RbxSerializer } from "@rbxts/message-templates/out/RbxSerializer";
import { ZirconFunction } from "./ZirconFunction";
import { $print } from "rbxts-transform-debug";

export class ZirconContext {
constructor(private innerContext: ZrContext, private executingFunction: ZirconFunction<any, any>) {}
Expand All @@ -23,9 +24,10 @@ export class ZirconContext {
import("../Services/LogService").then((log) => {
const message: Writable<LogEvent> = {
Level: level,
SourceContext: undefined,
SourceContext: `(function '${this.executingFunction.GetName()}')`,
Template: template,
Timestamp: DateTime.now().ToIsoDate(),
LogToPlayer: this.GetExecutor(),
};

const tokens = MessageTemplateParser.GetTokens(template);
Expand All @@ -46,13 +48,7 @@ export class ZirconContext {
}
}

log.ZirconLogService.WriteStructured({
Level: level,
SourceContext: `(function '${this.executingFunction.GetName()}')`,
LogToPlayer: this.GetExecutor(),
Template: template,
Timestamp: DateTime.now().ToIsoDate(),
});
log.ZirconLogService.WriteStructured(message);
});
}

Expand Down
53 changes: 30 additions & 23 deletions src/Class/ZirconFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,45 @@ import ZrLuauFunction, { ZrLuauArgument } from "@rbxts/zirconium/out/Data/LuauFu
import ZrPlayerScriptContext from "@rbxts/zirconium/out/Runtime/PlayerScriptContext";
import { $env } from "rbxts-transform-env";
import Server from "../Server";
import { ZirconFunctionBuilder } from "./ZirconFunctionBuilder";
import { InferArguments, Validator, ZirconValidator } from "./ZirconTypeValidator";
import { InferArguments, ZirconValidator } from "./ZirconTypeValidator";
import { ZirconContext } from "./ZirconContext";
import ZrUndefined from "@rbxts/zirconium/out/Data/Undefined";
import { $print } from "rbxts-transform-debug";

let zirconTypeOf: typeof import("Shared/typeId")["zirconTypeOf"] | undefined;

export function emitArgumentError(
func: ZirconFunction<any, any>,
context: ZrContext,
arg: number,
arg: ZrValue | ZrUndefined,
index: number,
validator: ZirconValidator<unknown, unknown>,
) {
const err = typeIs(validator.ErrorMessage, "function")
? validator.ErrorMessage(arg, index, func)
: validator.ErrorMessage;

// Have to dynamically import
if (zirconTypeOf === undefined) {
zirconTypeOf = import("Shared/typeId").expect().zirconTypeOf;
}

Server.Log.WriteStructured({
SourceContext: tostring(func),
SourceContext: `(function '${func.GetName()}')`,
Level: LogLevel.Error,
Template: `Call to {FunctionName} failed - Argument#{ArgIndex} expected {ArgType}`,
Template: `Argument #{ArgIndex} to '{FunctionName}': ${err ?? "Expected {ValidatorType}, got {ArgType}"}`,
Timestamp: DateTime.now().ToIsoDate(),
FunctionName: func.GetName(),
CallingPlayer: context.getExecutor()!,
ArgIndex: arg + 1,
ArgType: validator.Type,
FunctionArgs: func.GetArgumentTypes(),
FunctionVariadicArg: func.GetVariadicType(),
LogToPlayer: context.getExecutor(),
ArgIndex: index + 1,
ValidatorType: validator.Type,
ArgType: zirconTypeOf(arg),
});
}

export interface ZirconFunctionMetadata<V extends readonly ZirconValidator<unknown, unknown>[]> {
export interface ZirconFunctionMetadata {
readonly Description?: string;
readonly ArgumentValidators: ZirconValidator<unknown, unknown>[];
readonly VariadicValidator?: ZirconValidator<unknown, unknown>;
Expand All @@ -41,7 +57,7 @@ export class ZirconFunction<
public constructor(
private name: string,
private zirconCallback: (context: ZirconContext, ...args: InferArguments<V>) => R,
private metadata: ZirconFunctionMetadata<V>,
private metadata: ZirconFunctionMetadata,
) {
const { VariadicValidator, ArgumentValidators } = metadata;
super((context, ...args) => {
Expand All @@ -63,11 +79,8 @@ export class ZirconFunction<
}
} else {
if (RunService.IsServer()) {
emitArgumentError(this, context, i, validator);

if ($env("NODE_ENV") === "development") {
print("Got", argument);
}
emitArgumentError(this, context, argument, i, validator);
$print("Got", argument);
}
return;
}
Expand All @@ -87,10 +100,8 @@ export class ZirconFunction<
}
} else {
if (RunService.IsServer()) {
emitArgumentError(this, context, i, VariadicValidator);
if ($env("NODE_ENV") === "development") {
print("Got", argument);
}
emitArgumentError(this, context, argument, i, VariadicValidator);
$print("Got", argument);
}
return;
}
Expand Down Expand Up @@ -144,8 +155,4 @@ export class ZirconFunction<
") { [ZirconFunction] }"
);
}

public static args<V extends readonly Validator[]>(...value: V) {
return value;
}
}
11 changes: 11 additions & 0 deletions src/Class/ZirconTypeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { ZirconFuzzyPlayers } from "./Validators/ZirconFuzzyPlayersValidator";
import ZrRange from "@rbxts/zirconium/out/Data/Range";
import { ZirconFunction } from "./ZirconFunction";
import { zirconTypeOf } from "Shared/typeId";

type PickFrom<T, U> = U extends never ? T : U;
export interface ZirconValidator<T, U = never> {
Expand All @@ -26,6 +27,7 @@ export interface ZirconValidator<T, U = never> {
*/
Validate(value: unknown, player?: Player): value is T;
Transform?(value: T, player?: Player): U;
ErrorMessage?: ((arg: ZrValue | ZrUndefined, index: number, func: ZirconFunction<any, any>) => string) | string;
}

export interface ZirconArgument<T extends Validator> {
Expand All @@ -45,41 +47,47 @@ export const ZirconString: ZirconValidator<string> = {
Validate(value): value is string {
return typeIs(value, "string");
},
ErrorMessage: (value) => `Expected string, got ${zirconTypeOf(value)}`,
};

export const ZirconNumber: ZirconValidator<number> = {
Type: "number",
Validate(value): value is number {
return typeIs(value, "number");
},
ErrorMessage: (value) => `Expected number, got ${zirconTypeOf(value)}`,
};

export const ZirconBoolean: ZirconValidator<boolean> = {
Type: "boolean",
Validate(value): value is boolean {
return typeIs(value, "number");
},
ErrorMessage: (value) => `Expected boolean, got ${zirconTypeOf(value)}`,
};

export const ZirconObject: ZirconValidator<ZrObject> = {
Type: "object",
Validate(value): value is ZrObject {
return value instanceof ZrObject;
},
ErrorMessage: (value) => `Expected object, got ${zirconTypeOf(value)}`,
};

export const NativeEnum: ZirconValidator<ZrEnum> = {
Type: "ZrEnum",
Validate(value: unknown): value is ZrEnum {
return value instanceof ZrEnum;
},
ErrorMessage: (value) => `Expected enum, got ${zirconTypeOf(value)}`,
};

export const NativeEnumItem: ZirconValidator<ZrEnumItem> = {
Type: "ZrEnumItem",
Validate(value: unknown): value is ZrEnumItem {
return value instanceof ZrEnumItem;
},
ErrorMessage: (value) => `Expected enum item, got ${zirconTypeOf(value)}`,
};

export function ZirconOptional<K extends ZirconValidator<ZrValue, unknown>>(validator: K) {
Expand Down Expand Up @@ -114,6 +122,7 @@ export const ZirconDefined: ZirconValidator<ZrValue> = {
Validate(value: unknown): value is ZrValue {
return value !== ZrUndefined && value !== undefined;
},
ErrorMessage: (value) => `Expected defined, got ${zirconTypeOf(value)}`,
};

export const ZirconRange: ZirconValidator<ZrRange | number, ZrRange> = {
Expand All @@ -128,6 +137,7 @@ export const ZirconRange: ZirconValidator<ZrRange | number, ZrRange> = {
return value;
}
},
ErrorMessage: (value) => `Expected range, got ${zirconTypeOf(value)}`,
};

export function ZirconInstanceIsA<K extends keyof Instances>(typeName: K) {
Expand All @@ -139,6 +149,7 @@ export function ZirconInstanceIsA<K extends keyof Instances>(typeName: K) {
Transform(value) {
return value.value() as Instances[K];
},
ErrorMessage: (value) => `Expected Instance, got ${zirconTypeOf(value)}`,
});
}

Expand Down
28 changes: 22 additions & 6 deletions src/Client/BuiltInConsole/UI/DockedConsole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Workspace } from "@rbxts/services";
import Padding from "Client/Components/Padding";
import SearchTextBox from "Client/Components/SearchTextBox";
import MultiSelectDropdown from "Client/Components/MultiSelectDropdown";
import { $print } from "rbxts-transform-debug";

export interface DockedConsoleProps extends MappedProps, MappedDispatch {}
interface DockedConsoleState {
Expand Down Expand Up @@ -293,7 +294,9 @@ class ZirconConsoleComponent extends Roact.Component<DockedConsoleProps, DockedC
OnClick={() => {}}
/>
<ZirconSyntaxTextBox
RefocusOnSubmit={this.props.autoFocus}
AutoFocus={this.props.autoFocus}
PlaceholderText="Enter script to execute"
Size={new UDim2(1, -16 - 32 - 100, 1, 0)}
Position={new UDim2(0, 16, 0, 0)}
Focused={this.state.isVisible}
Expand All @@ -305,19 +308,32 @@ class ZirconConsoleComponent extends Roact.Component<DockedConsoleProps, DockedC
}}
OnHistoryTraversal={(direction) => {
let index = this.state.historyIndex;

const history = this.props.history;
let text = "";
if (direction === "back") {
index = this.state.historyIndex - 1;
if (index <= 0) {
index = history.size() - 1;
} else {
index = index - 1;
}

text = history[index];
} else if (direction === "forward") {
index = this.state.historyIndex + 1;
if (index >= history.size() - 1) {
index = 0;
} else {
index = index + 1;
}

text = history[index];
}

print("[historyTraversal]", direction);
$print("[historyTraversal]", direction, text, history);

this.setState({
historyIndex: index,
source: this.props.history[
index < 0 ? this.props.history.size() - index : index
],
source: text,
});
}}
/>
Expand Down
Loading

0 comments on commit 6be55f7

Please sign in to comment.