-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mol cmd with mol-related tools (#127)
- Loading branch information
Showing
13 changed files
with
421 additions
and
3 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
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 @@ | ||
import basic_types; | ||
|
||
// Each role has 8 attributes. The size is fixed. | ||
struct Attributes { | ||
strength: AttrValue, | ||
dexterity: AttrValue, | ||
endurance: AttrValue, | ||
speed: AttrValue, | ||
intelligence: AttrValue, | ||
wisdom: AttrValue, | ||
perception: AttrValue, | ||
concentration: AttrValue, | ||
} |
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,25 @@ | ||
// AttrValue is an alias of `byte`. | ||
// | ||
// Since Molecule data are strongly-typed, it can gives compile time guarantees | ||
// that the right type of value is supplied to a method. | ||
// | ||
// In this example, we use this alias to define an unsigned integer which | ||
// has an upper limit: 100. | ||
// So it's easy to distinguish between this type and a real `byte`. | ||
// Of course, the serialization wouldn't do any checks for this upper limit | ||
// automatically. You have to implement it by yourself. | ||
// | ||
// **NOTE**: | ||
// - This feature is dependent on the exact implementation. | ||
// In official Rust generated code, we use new type to implement this feature. | ||
array AttrValue [byte; 1]; | ||
|
||
// SkillLevel is an alias of `byte`, too. | ||
// | ||
// Each skill has only 10 levels, so we use another alias of `byte` to distinguish. | ||
array SkillLevel [byte; 1]; | ||
|
||
// Define several unsigned integers. | ||
array Uint8 [byte; 1]; | ||
array Uint16 [byte; 2]; | ||
array Uint32 [byte; 4]; |
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,22 @@ | ||
import attributes; | ||
import skills; | ||
import basic_types; | ||
|
||
// We have only 3 classes: Fighter, Ranger and Mage. A `byte` is enough. | ||
array Class [byte; 1]; | ||
|
||
table Hero { | ||
class: Class, | ||
level: Uint8, | ||
experiences: Uint32, | ||
hp: Uint16, | ||
mp: Uint16, | ||
base_damage: Uint16, | ||
attrs: Attributes, | ||
skills: Skills, | ||
} | ||
|
||
table Monster { | ||
hp: Uint16, | ||
damage: Uint16, | ||
} |
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,33 @@ | ||
import basic_types; | ||
|
||
// We define several skills. | ||
// None means the role can learn a skill but he/she doesn't learn it. | ||
option ArmorLight (SkillLevel); | ||
option ArmorHeavy (SkillLevel); // only Fighter can learn this | ||
option ArmorShields (SkillLevel); // only Fighter can learn this | ||
option WeaponSwords (SkillLevel); // only Mage can't learn this | ||
option WeaponBows (SkillLevel); // only Ranger can learn this | ||
option WeaponBlunt (SkillLevel); | ||
option Dodge (SkillLevel); | ||
option PickLocks (SkillLevel); | ||
option Mercantile (SkillLevel); | ||
option Survival (SkillLevel); | ||
// ... omit other skills ... | ||
|
||
// Any skill which is defined above. | ||
union Skill { | ||
ArmorLight, | ||
ArmorHeavy, | ||
ArmorShields, | ||
WeaponSwords, | ||
WeaponBows, | ||
WeaponBlunt, | ||
Dodge, | ||
PickLocks, | ||
Mercantile, | ||
Survival, | ||
// ... omit other skills ... | ||
} | ||
|
||
// A hero can learn several skills. The size of learned skills is dynamic. | ||
vector Skills <Skill>; |
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,22 @@ | ||
import { BindingLanguage, generateMolBindings } from '../molecule/mol'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export async function molSingleFile(schemeFilePath: string, outputFilePath: string, bindingLang: string) { | ||
await generateMolBindings(schemeFilePath, outputFilePath, bindingLang as BindingLanguage); | ||
} | ||
|
||
export async function molFiles(schemaFolderPath: string, outputFolderPath: string, bindingLang: string) { | ||
const files = fs.readdirSync(schemaFolderPath).filter((file) => file.endsWith('.mol')); | ||
|
||
if (files.length === 0) { | ||
throw new Error(`No .mol files found in the specified folder: ${schemaFolderPath}`); | ||
} | ||
|
||
for (const file of files) { | ||
const filePath = path.join(schemaFolderPath, file); | ||
const outputFileName = path.basename(file, '.mol') + '.' + bindingLang; | ||
const outputFilePath = path.join(outputFolderPath, outputFileName); | ||
await molSingleFile(filePath, outputFilePath, bindingLang as BindingLanguage); | ||
} | ||
} |
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,119 @@ | ||
import { execSync } from 'child_process'; | ||
import { MoleculecES } from '../tools/moleculec-es'; | ||
import { MoleculecRust } from '../tools/moleculec-rust'; | ||
import { readSettings } from '../cfg/setting'; | ||
import path from 'path'; | ||
import * as fs from 'fs'; | ||
import { MoleculecGo } from '../tools/moleculec-go'; | ||
|
||
export enum BindingLanguage { | ||
rust = 'rs', | ||
c = 'c', | ||
typescript = 'ts', | ||
javascript = 'js', | ||
go = 'go', | ||
} | ||
|
||
export async function generateMolBindings( | ||
schemeFilePath: string, | ||
outputFilePath: string | undefined, | ||
bindingLanguage: BindingLanguage, | ||
) { | ||
await installMolToolsIfNeeded(); | ||
const settings = readSettings(); | ||
if (bindingLanguage === BindingLanguage.typescript) { | ||
const jsonFilePath = path.join(settings.tools.moleculeES.cachePath, 'schema.json'); | ||
fs.mkdirSync(path.dirname(jsonFilePath), { recursive: true }); | ||
if (outputFilePath) { | ||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
} | ||
|
||
execSync(`moleculec --language - --schema-file ${schemeFilePath} --format json > ${jsonFilePath}`); | ||
execSync( | ||
`${MoleculecES.bin} -generateTypeScriptDefinition -hasBigInt -inputFile ${jsonFilePath} -outputFile ${outputFilePath || '-'}`, | ||
{ stdio: 'inherit' }, | ||
); | ||
return; | ||
} | ||
|
||
if (bindingLanguage === BindingLanguage.javascript) { | ||
const jsonFilePath = path.join(settings.tools.moleculeES.cachePath, 'schema.json'); | ||
fs.mkdirSync(path.dirname(jsonFilePath), { recursive: true }); | ||
if (outputFilePath) { | ||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
} | ||
|
||
execSync(`moleculec --language - --schema-file ${schemeFilePath} --format json > ${jsonFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
execSync(`${MoleculecES.bin} -hasBigInt -inputFile ${jsonFilePath} -outputFile ${outputFilePath || '-'}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
if (bindingLanguage === BindingLanguage.c) { | ||
if (!outputFilePath) { | ||
execSync(`moleculec --language c --schema-file ${schemeFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
|
||
execSync(`moleculec --language c --schema-file ${schemeFilePath} > ${outputFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
if (bindingLanguage === BindingLanguage.rust) { | ||
if (!outputFilePath) { | ||
execSync(`moleculec --language rust --schema-file ${schemeFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
|
||
execSync(`moleculec --language rust --schema-file ${schemeFilePath} > ${outputFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
if (bindingLanguage === BindingLanguage.go) { | ||
if (!outputFilePath) { | ||
execSync(`moleculec --language go --schema-file ${schemeFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true }); | ||
|
||
execSync(`moleculec --language go --schema-file ${schemeFilePath} | gofmt > ${outputFilePath}`, { | ||
stdio: 'inherit', | ||
}); | ||
return; | ||
} | ||
|
||
throw new Error(`Unsupported binding language: ${bindingLanguage}`); | ||
} | ||
|
||
export async function installMolToolsIfNeeded() { | ||
if (!MoleculecES.isBinaryInstalled()) { | ||
const version = '0.4.6'; | ||
await MoleculecES.installMoleculeES(version); | ||
} | ||
|
||
if (!MoleculecRust.isBinaryInstalled()) { | ||
MoleculecRust.installBinary(); | ||
} | ||
|
||
if (!MoleculecGo.isBinaryInstalled()) { | ||
MoleculecGo.installBinary(); | ||
} | ||
} |
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.