Skip to content

Commit 57333b6

Browse files
committed
数据包库文件现在是输出为二进制
1 parent e2eb48a commit 57333b6

File tree

117 files changed

+2134
-1077
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+2134
-1077
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import java.nio.file.Files
32
import java.util.Optional
43

@@ -47,6 +46,7 @@ dependencies {
4746
implementation(kotlin("reflect"))
4847
testImplementation(kotlin("script-runtime"))
4948
implementation("com.google.guava:guava:33.2.0-jre")
49+
implementation("com.esotericsoftware:kryo:5.6.2")
5050
}
5151

5252
tasks.shadowJar {

src/main/antlr/mcfppParser.g4

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ options {
3232
tokenVocab = mcfppLexer;
3333
}
3434

35-
//TODO 类型别名和导入库文件的语句顺序不用这么严格
3635
//一个mcfpp文件
3736
compilationUnit
3837
: namespaceDeclaration?
3938
importDeclaration*
40-
topStatement
39+
//TODO topStatement
4140
typeDeclaration*
4241
EOF
4342
;
@@ -75,14 +74,19 @@ declarations
7574
| objectTemplateDeclaration
7675
| extensionFunctionDeclaration
7776
| interfaceDeclaration
78-
| globalDeclaration
77+
| namespaceFieldDeclaration
7978
| enumDeclaration
8079
| annotation
8180
;
8281

83-
//全局声明
84-
globalDeclaration
85-
: GLOBAL '{' (doc_comment? fieldDeclaration ';')* '}'
82+
//命名空间变量声明
83+
namespaceFieldDeclaration
84+
: fieldModifier? type namespaceFieldDeclarationExpression (',' namespaceFieldDeclarationExpression)*
85+
| fieldModifier? VAR Identifier '=' value
86+
;
87+
88+
namespaceFieldDeclarationExpression
89+
: Identifier ( '=' value)?
8690
;
8791

8892
//类声明
@@ -180,8 +184,6 @@ templateMember
180184
: templateFunctionDeclaration
181185
| templateFieldDeclaration
182186
| templateConstructorDeclaration
183-
| innerTemplateDeclaration
184-
| innerTemplateListDeclaration
185187
| annotation
186188
;
187189

@@ -201,14 +203,6 @@ unionTemplateFieldType
201203
: '(' type (PIPE type)* ')' QUEST?
202204
;
203205

204-
innerTemplateDeclaration
205-
: DATA Identifier (COLON className (',' className)*)? templateBody
206-
;
207-
208-
innerTemplateListDeclaration
209-
: LIST '<' DATA '>' Identifier (COLON className (',' className)*)? templateBody
210-
;
211-
212206
//接口声明
213207
interfaceDeclaration
214208
: INTERFACE classWithoutNamespace (ARROW className (',' className)*)? interfaceBody
@@ -568,6 +562,11 @@ typeWithoutExcl
568562
| className readOnlyArgs?
569563
| Identifier
570564
| unionTemplateType
565+
| anonymousTemplateType
566+
;
567+
568+
anonymousTemplateType
569+
: DATA (COLON className (',' className)*)? templateBody
571570
;
572571

573572
unionTemplateType

src/main/kotlin/top/mcfpp/Project.kt

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ import top.mcfpp.core.lang.ClassPointer
1515
import top.mcfpp.core.lang.MCFloat
1616
import top.mcfpp.core.lang.UnresolvedVar
1717
import top.mcfpp.core.lang.Var
18-
import top.mcfpp.io.LibReader
19-
import top.mcfpp.io.LibWriter
18+
import top.mcfpp.io.LibBinReader
19+
import top.mcfpp.io.LibBinWriter
2020
import top.mcfpp.io.MCFPPFile
2121
import top.mcfpp.lib.SbObject
2222
import top.mcfpp.model.Native
2323
import top.mcfpp.model.ObjectClass
2424
import top.mcfpp.model.field.GlobalField
2525
import top.mcfpp.model.function.Function
26+
import top.mcfpp.model.function.NativeFunction
2627
import top.mcfpp.util.LogProcessor
2728
import top.mcfpp.util.Utils
2829
import java.io.File
2930
import java.io.FileReader
3031
import java.io.IOException
3132
import java.net.URLClassLoader
32-
import java.nio.charset.StandardCharsets
3333
import java.nio.file.Path
3434
import java.nio.file.Paths
3535
import java.util.jar.JarFile
@@ -74,18 +74,6 @@ object Project {
7474

7575
lateinit var mcfppInit : Function
7676

77-
val mcfppSystemTick: Function = Function("sys.tick","mcfpp", null).apply {
78-
commands.addAll(
79-
arrayOf(
80-
//指针清理
81-
Command("execute as @e[type=marker,tag=mcfpp_ptr] if score @s ${SbObject.MCFPP_POINTER_COUNTER} matches ..0 run kill @s"),
82-
//内存泄露检查
83-
Command("execute if data storage mcfpp:system stack_frame[0] run tellraw @a {\"text\":\"[MCFPP]Stack Leak\"}"),
84-
Command("execute if data storage mcfpp:system stack_frame[0] run data modify storage mcfpp:system stack_frame set value []"),
85-
)
86-
)
87-
}
88-
8977
/**
9078
* 常量池
9179
*/
@@ -98,15 +86,25 @@ object Project {
9886

9987
var compileStage = 0
10088

89+
@Suppress("MemberVisibilityCanBePrivate")
10190
const val PRE_INIT = 0
91+
@Suppress("MemberVisibilityCanBePrivate")
10292
const val INIT = PRE_INIT + 1
93+
@Suppress("MemberVisibilityCanBePrivate")
10394
const val READ_LIB = INIT + 1
95+
@Suppress("MemberVisibilityCanBePrivate")
10496
const val INDEX_TYPE = READ_LIB + 1
97+
@Suppress("MemberVisibilityCanBePrivate")
10598
const val RESOLVE_FIELD = INDEX_TYPE + 1
99+
@Suppress("MemberVisibilityCanBePrivate")
106100
const val RUN_ANNOTATION = RESOLVE_FIELD + 1
101+
@Suppress("MemberVisibilityCanBePrivate")
107102
const val COMPILE = RUN_ANNOTATION + 1
103+
@Suppress("MemberVisibilityCanBePrivate")
108104
const val OPTIMIZATION = COMPILE + 1
105+
@Suppress("MemberVisibilityCanBePrivate")
109106
const val GEN_INDEX = OPTIMIZATION + 1
107+
@Suppress("MemberVisibilityCanBePrivate")
110108
const val GEN_DATAPACK = GEN_INDEX + 1
111109

112110
/**
@@ -116,7 +114,19 @@ object Project {
116114

117115
var classLoader: ClassLoader = Thread.currentThread().contextClassLoader
118116

119-
val files = ArrayList<MCFPPFile>()
117+
private val files = ArrayList<MCFPPFile>()
118+
119+
val mcfppSystemTick: Function = Function("sys.tick","mcfpp", null).apply {
120+
commands.addAll(
121+
arrayOf(
122+
//指针清理
123+
Command("execute as @e[type=marker,tag=mcfpp_ptr] if score @s ${SbObject.MCFPP_POINTER_COUNTER} matches ..0 run kill @s"),
124+
//内存泄露检查
125+
Command("execute if data storage mcfpp:system stack_frame[0] run tellraw @a {\"text\":\"[MCFPP]Stack Leak\"}"),
126+
Command("execute if data storage mcfpp:system stack_frame[0] run data modify storage mcfpp:system stack_frame set value []"),
127+
)
128+
)
129+
}
120130

121131
/**
122132
* 初始化
@@ -264,17 +274,13 @@ object Project {
264274
}
265275
//默认的
266276
if(!CompileSettings.ignoreStdLib){
267-
val inputStream = ResourceReader::class.java.classLoader.getResourceAsStream("lib/.mclib")
277+
val inputStream = ResourceReader::class.java.classLoader.getResourceAsStream("lib/bin.mclib")
268278

269279
if (inputStream == null) {
270-
LogProcessor.error("Cannot find lib file at: lib/.mclib")
280+
LogProcessor.error("Cannot find lib file at: lib/bin.mclib")
271281
return
272282
}
273-
274-
// 读取文件内容
275-
val fileContent = String(inputStream.readAllBytes(), StandardCharsets.UTF_8)
276-
// 输出文件内容
277-
LibReader.readFromString(fileContent)
283+
LibBinReader.readFromStream(inputStream)
278284
}
279285
//写入缓存
280286
for (include in config.includes) {
@@ -283,13 +289,10 @@ object Project {
283289
if(file.exists()){
284290
try {
285291
JarFile(filePath).use { jarFile ->
286-
val jarEntry = jarFile.getJarEntry("lib/.mclib")
292+
val jarEntry = jarFile.getJarEntry("lib/bin.mclib")
287293
if (jarEntry != null) {
288-
jarFile.getInputStream(jarEntry).use { inputStream ->
289-
// 读取文件内容
290-
val fileContent = String(inputStream.readAllBytes(), StandardCharsets.UTF_8)
291-
// 输出文件内容
292-
LibReader.readFromString(fileContent)
294+
jarFile.getInputStream(jarEntry).use {
295+
LibBinReader.readFromStream(it)
293296
}
294297
} else {
295298
LogProcessor.error("Cannot find lib file at: ${file.absolutePath}")
@@ -313,6 +316,17 @@ object Project {
313316
}
314317
}
315318
}
319+
namespace.field.forEachFunction { f ->
320+
run {
321+
if(f is NativeFunction){
322+
//找到方法
323+
val clazz = f.javaMethodName.substringBeforeLast(".")
324+
val methodName = f.javaMethodName.substringAfterLast(".")
325+
val clazzObject = Class.forName(clazz)
326+
f.javaMethod = clazzObject.getMethod(methodName)
327+
}
328+
}
329+
}
316330
}
317331
//实例化所有类中的成员字段
318332
for(namespace in GlobalField.stdNamespaces.values){
@@ -522,7 +536,7 @@ object Project {
522536
*/
523537
fun genIndex() {
524538
compileStage++
525-
LibWriter.write(config.targetPath!!.absolutePathString())
539+
LibBinWriter.write(config.targetPath!!.absolutePathString())
526540
stageProcessor[compileStage].forEach { it() }
527541
}
528542
}

src/main/kotlin/top/mcfpp/antlr/MCFPPAnnotationVisitor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import top.mcfpp.model.function.FunctionParam
1212
import top.mcfpp.util.LogProcessor
1313
import top.mcfpp.util.NBTUtil.toJava
1414
import top.mcfpp.util.StringHelper
15+
import top.mcfpp.util.StringHelper.splitNamespaceID
1516

1617
class MCFPPAnnotationVisitor: mcfppParserBaseVisitor<Unit>(){
1718

@@ -20,7 +21,7 @@ class MCFPPAnnotationVisitor: mcfppParserBaseVisitor<Unit>(){
2021
override fun visitAnnotation(ctx: mcfppParser.AnnotationContext?) {
2122
Project.ctx = ctx
2223
//获取注解
23-
val qwq = StringHelper.splitNamespaceID(ctx!!.Identifier().text)
24+
val qwq = ctx!!.Identifier().text.splitNamespaceID()
2425
val annotation = GlobalField.getAnnotation(qwq.first, qwq.second)
2526
if(annotation == null){
2627
//注解不存在

src/main/kotlin/top/mcfpp/antlr/MCFPPExprVisitor.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ import top.mcfpp.type.MCFPPBaseType
2323
import top.mcfpp.type.MCFPPEnumType
2424
import top.mcfpp.type.MCFPPGenericClassType
2525
import top.mcfpp.type.MCFPPType
26-
import top.mcfpp.util.*
26+
import top.mcfpp.util.BoolTag
27+
import top.mcfpp.util.LogProcessor
2728
import top.mcfpp.util.NBTUtil.toNBTByte
2829
import top.mcfpp.util.NBTUtil.toNBTDouble
2930
import top.mcfpp.util.NBTUtil.toNBTFloat
3031
import top.mcfpp.util.NBTUtil.toNBTLong
3132
import top.mcfpp.util.NBTUtil.toNBTShort
33+
import top.mcfpp.util.StringHelper.splitNamespaceID
34+
import top.mcfpp.util.TempPool
35+
import top.mcfpp.util.TextTranslator
3236
import top.mcfpp.util.TextTranslator.translate
3337
import java.util.*
3438

@@ -230,7 +234,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
230234
override fun visitCastExpression(ctx: mcfppParser.CastExpressionContext): Var<*> {
231235
Project.ctx = ctx
232236
val a: Var<*> = visitUnaryExpression(ctx.unaryExpression())
233-
return a.explicitCast(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field)?: run {
237+
return a.explicitCast(MCFPPType.parseFromString(ctx.type().text, Function.currFunction.field)?: run {
234238
LogProcessor.error(TextTranslator.INVALID_TYPE_ERROR.translate(ctx.type().text))
235239
MCFPPBaseType.Any
236240
})
@@ -266,7 +270,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
266270
currSelector = visitJvmAccessExpression(ctx.jvmAccessExpression())
267271
if(currSelector is UnknownVar){
268272
val typeStr = ctx.jvmAccessExpression().text
269-
val type = MCFPPType.parseFromIdentifier(typeStr, Function.currFunction.field)
273+
val type = MCFPPType.parseFromString(typeStr, Function.currFunction.field)
270274
if(type == null){
271275
LogProcessor.error(TextTranslator.VARIABLE_NOT_DEFINED.translate(currSelector!!.identifier))
272276
}else{
@@ -275,7 +279,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
275279
}
276280
}else{
277281
val typeStr = ctx.type().text
278-
val type = MCFPPType.parseFromIdentifier(typeStr, Function.currFunction.field)
282+
val type = MCFPPType.parseFromString(typeStr, Function.currFunction.field)
279283
if(type == null){
280284
if(ctx.selector().size == 0){
281285
LogProcessor.error(TextTranslator.VARIABLE_NOT_DEFINED.translate(currSelector!!.identifier))
@@ -352,7 +356,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
352356
return UnknownVar("range_" + UUID.randomUUID())
353357
}
354358
} else if (ctx.type() != null){
355-
return MCFPPTypeVar(MCFPPType.parseFromIdentifier(ctx.type().text, Function.currFunction.field)?: run {
359+
return MCFPPTypeVar(MCFPPType.parseFromString(ctx.type().text, Function.currFunction.field)?: run {
356360
LogProcessor.error(TextTranslator.INVALID_TYPE_ERROR.translate(ctx.type().text))
357361
MCFPPBaseType.Any
358362
})
@@ -417,7 +421,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
417421
normalArgs.add(arg)
418422
}
419423
//获取函数
420-
val p = StringHelper.splitNamespaceID(ctx.namespaceID().text)
424+
val p = ctx.namespaceID().text.splitNamespaceID()
421425
val func = if(currSelector == null){
422426
GlobalField.getFunction(p.first, p.second, readOnlyArgs, normalArgs)
423427
}else{
@@ -553,6 +557,7 @@ class MCFPPExprVisitor(private var defaultGenericClassType : MCFPPGenericClassTy
553557
}
554558
}
555559

560+
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE", "UNUSED_VALUE")
556561
override fun visitValue(ctx: mcfppParser.ValueContext): Var<*> {
557562
//常量
558563
if (ctx.LineString() != null) {

0 commit comments

Comments
 (0)