文件编译系统升级

This commit is contained in:
coldmint 2022-04-24 22:20:26 +08:00
parent 5d4f078664
commit 61cf34c513
21 changed files with 241 additions and 41 deletions

Binary file not shown.

View File

@ -20,6 +20,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
import com.afollestad.materialdialogs.input.getInputField
import com.afollestad.materialdialogs.input.input
import com.afollestad.materialdialogs.list.ItemListener
@ -1180,6 +1181,13 @@ class EditActivity : BaseActivity<ActivityEditBinding>() {
goIntent.putExtra("filePath", filePath)
startActivity(goIntent)
}
R.id.display_source_code -> {
val file = File(viewModel.getNowOpenFilePath())
val code = FileOperator.readFile(file)
MaterialDialog(this, BottomSheet()).show {
title(text = file.name).message(text = code).negativeButton(R.string.dialog_ok)
}
}
R.id.clear_code_cache -> {
viewModel.codeCompiler2.clearCache()
Snackbar.make(

View File

@ -94,6 +94,7 @@ class WebModInfoActivity : BaseActivity<ActivityWebModInfoBinding>() {
}
modId = temId
if (targetFile.exists()) {
viewBinding.button.isEnabled = false
viewBinding.button.text = getString(R.string.installated)
}
viewBinding.modCommentRecyclerView.layoutManager =
@ -375,6 +376,7 @@ class WebModInfoActivity : BaseActivity<ActivityWebModInfoBinding>() {
if (materialDialog.isShowing) {
materialDialog.dismiss()
}
viewBinding.button.isEnabled = false
viewBinding.button.setText(R.string.installated)
WebMod.instance.addDownloadNum(modId)
}

View File

@ -8,6 +8,7 @@ import com.coldmint.rust.core.database.code.CodeDataBase
import com.coldmint.rust.core.database.code.SectionInfo
import com.coldmint.rust.core.database.file.FileDataBase
import com.coldmint.rust.core.interfaces.EnglishMode
import com.coldmint.rust.core.tool.DebugHelper
import com.coldmint.rust.core.tool.LineParser
import com.coldmint.rust.pro.tool.AppSettings
import com.coldmint.rust.pro.tool.CompletionItemConverter
@ -27,6 +28,7 @@ class RustAutoComplete2(val context: Context) : AutoCompleteProvider, EnglishMod
private val result: ArrayList<CompletionItem> by lazy {
ArrayList()
}
private val debugKey = "自动完成器"
private var codeDataBase: CodeDataBase? = null
private val executorService = Executors.newCachedThreadPool()
private var fileDataBase: FileDataBase? = null
@ -63,6 +65,7 @@ class RustAutoComplete2(val context: Context) : AutoCompleteProvider, EnglishMod
* @param sourceFolder String
*/
fun setSourceFolder(sourceFolder: String) {
DebugHelper.printLog(debugKey, "已设置源文件目录" + sourceFolder, "设置源文件目录")
completionItemConverter.setSourceFilePath(sourceFolder)
}
@ -214,33 +217,55 @@ class RustAutoComplete2(val context: Context) : AutoCompleteProvider, EnglishMod
} else {
lineData.subSequence(0, keyIndex)
}
val codeInfo =
if (isEnglishMode) {
codeDataBase!!.getCodeDao().findCodeByCode(keyValue.toString())
} else {
codeDataBase!!.getCodeDao().findCodeByTranslate(keyValue.toString())
}
DebugHelper.printLog(
debugKey,
"值[" + keyValue + "]英文模式[" + isEnglishMode + "]代码信息[" + codeInfo + "]关键字[" + prefix + "]",
"值检查"
)
if (codeInfo != null) {
val typeInfo = completionItemConverter.getValueType(codeInfo.type)
//获取代码的关联列表
if (typeInfo != null && typeInfo.list.isNotBlank()) {
DebugHelper.printLog(
debugKey,
"值类型[" + codeInfo.type + "]自动提示列表[" + typeInfo.list + "]", "关联提示"
)
lineParser.text = typeInfo.list
lineParser.analyse { lineNum, lineData, isEnd ->
//分析关联提示项目
val temCodeInfo =
codeDataBase!!.getCodeDao().findCodeByCode(lineData)
DebugHelper.printLog(
debugKey,
"值类型[" + codeInfo.type + "]项目[" + lineData + "]是代码[" + (temCodeInfo != null) + "]",
"关联提示列表分析"
)
if (temCodeInfo == null) {
if (lineData.startsWith("@file(") && lineData.endsWith(")")) {
val fileType = lineData.subSequence(
lineData.indexOf('(') + 1,
lineData.indexOf(')')
)
val fileInfo = fileDataBase!!.getFileInfoDao()
.searchFileInfoByNameAndType(
prefix,
fileType.toString(),
identifiersPromptNumber
)
DebugHelper.printLog(
debugKey,
"值类型[" + codeInfo.type + "]项目[" + lineData + "]搜索了[" + fileType + "]类型的文件,返回了[" + (fileInfo?.size
?: -1) + "]个结果",
"关联提示列表分析"
)
if (fileInfo != null && fileInfo.isNotEmpty()) {
for (fileTable in fileInfo) {
result.add(
@ -277,17 +302,34 @@ class RustAutoComplete2(val context: Context) : AutoCompleteProvider, EnglishMod
lineData.indexOf('(') + 1,
lineData.indexOf(')')
).toString()
codeDataBase!!.getCodeDao().findCodeByKeyInType(
val list = if (isEnglishMode
) {
codeDataBase!!.getCodeDao().findCodeByCodeInType(
prefix,
type,
identifiersPromptNumber
)?.forEach {
)
} else {
codeDataBase!!.getCodeDao().findCodeByTranslateInType(
prefix,
type,
identifiersPromptNumber
)
}
DebugHelper.printLog(
debugKey,
"关联了值类型[" + type + "]获取[" + (list?.size ?: -1) + "]个结果",
"值类型引用"
)
if (!list.isNullOrEmpty()) {
list.forEach {
result.add(
completionItemConverter.codeInfoToCompletionItem(
it
)
)
}
}
} else if (lineData.startsWith("@section") && lineData.endsWith(
")"
)
@ -318,7 +360,18 @@ class RustAutoComplete2(val context: Context) : AutoCompleteProvider, EnglishMod
)
}
} else {
if (temCodeInfo.translate.contains(prefix)) {
val show = if (isEnglishMode) {
temCodeInfo.code.contains(prefix)
} else {
temCodeInfo.translate.contains(prefix)
}
DebugHelper.printLog(
debugKey,
"值类型[" + codeInfo.type + "]项目[" + lineData + "]是否包含" + prefix + "关键字[" + show + "]",
"关联提示列表分析"
)
if (show) {
result.add(
completionItemConverter.codeInfoToCompletionItem(
temCodeInfo

View File

@ -39,9 +39,9 @@
android:title="@string/word_wrap" />
<item
android:visible="false"
android:id="@+id/turret_design"
android:title="@string/turret_design" />
android:title="@string/turret_design"
android:visible="false" />
<item
android:id="@+id/search_view"
@ -54,6 +54,9 @@
<item
android:id="@+id/clear_code_cache"
android:title="@string/clear_code_cache" />
<item
android:id="@+id/display_source_code"
android:title="@string/display_source_code" />
</menu>

View File

@ -827,4 +827,5 @@
<string name="email_fill_complete">邮箱地址填充完成。</string>
<string name="confirm_password">确认密码</string>
<string name="confirm_password_error">两次密码不一致。</string>
<string name="display_source_code">显示源代码</string>
</resources>

View File

@ -4,11 +4,14 @@ import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import com.coldmint.rust.core.dataBean.CompileConfiguration
import com.coldmint.rust.core.database.code.CodeDataBase
import com.coldmint.rust.core.database.code.SectionInfo
import com.coldmint.rust.core.database.code.ValueTypeInfo
import com.coldmint.rust.core.interfaces.*
import com.coldmint.rust.core.tool.DebugHelper
import com.coldmint.rust.core.tool.FileOperator
import com.coldmint.rust.core.tool.LineParser
import java.io.File
@ -70,6 +73,7 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
companion object {
const val split = "\n ,:()=%{}+*/\r"
val debugKey = "代码编译器"
@SuppressLint("StaticFieldLeak")
private var instance: CodeCompiler2? = null
@ -175,7 +179,7 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
if (translationMap.containsKey(code)) {
translationResult.append(translationMap[code])
} else {
codeResult.delete(0, codeResult.length)
codeResult.clear()
when (code) {
"\n" -> {
if (codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
@ -184,9 +188,10 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
codeDataBase.getCodeDao().findCodeByCode(referenceValue)
if (codeInfo == null) {
translationResult.append(referenceResult)
}else{
// tr
} else {
translationResult.append(codeInfo.translate)
}
referenceResult.clear()
}
codeBlockType = CompileConfiguration.CodeBlockType.Key
codeResult.append(code)
@ -295,6 +300,11 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
translationMap[code] = codeResult.toString()
}
translationResult.append(codeResult.toString())
DebugHelper.printLog(
debugKey,
"代码[" + code + "]译文[" + codeResult.toString() + "]是否翻译[" + (code != codeResult.toString()) + "]",
"翻译"
)
}
}
handler.post {
@ -323,6 +333,11 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
val codeResult = StringBuilder()
//保存完整的翻译结果
val compileResult = StringBuilder()
//是否为首次引用冒号值(首次值会直接附加到结果集合内)
var isFirstReference = false
//保存资源引用值(应该看做整体处理)
val referenceResult = StringBuilder()
val context = compileConfiguration.context
handler.post {
compilerListener?.beforeCompilation()
@ -330,7 +345,7 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
val startTime = System.currentTimeMillis()
while (tokenizer.hasMoreTokens()) {
val translation = tokenizer.nextToken()
codeResult.delete(0, codeResult.length)
codeResult.clear()
if (compileMap.containsKey(translation)) {
//此处仅读取字段缓存,不涉及错误缓存的读取
val code = compileMap[translation]
@ -351,32 +366,67 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
} else {
when (translation) {
"\n" -> {
analysisLineCode(compileConfiguration, compilerListener)
if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
//此段代码将临时引用数据referenceValue 附加到编译结果内,并附加到 值 数据内(附加值数据用于检查)
val referenceValue = referenceResult.toString()
compileConfiguration.appendValue(referenceResult.toString())
val codeInfo = codeDataBase.getCodeDao()
.findCodeByTranslate(referenceResult.toString())
if (codeInfo == null) {
compileResult.append(referenceValue)
} else {
compileResult.append(codeInfo.code)
}
DebugHelper.printLog(
debugKey,
"引用数据[" + referenceValue + "]代码信息存在状态[" + (codeInfo != null) + "]",
"行引用附加"
)
referenceResult.clear()
}
isFirstReference = true
checkLineCode(compileConfiguration, compilerListener)
compileConfiguration.nextLine()
codeResult.append(translation)
}
"\r" -> {
}
" ", ",", "(", ")", "=", "%", "{", "}", "+", "*", "/" -> {
if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
referenceResult.append(translation)
} else {
codeResult.append(
translation
)
compileConfiguration.appendResult(translation)
}
}
":" -> {
when (compileConfiguration.codeBlockType) {
CompileConfiguration.CodeBlockType.Value -> {
compileConfiguration.appendResult(translation)
codeResult.append(translation)
}
CompileConfiguration.CodeBlockType.Key -> {
compileConfiguration.codeBlockType =
CompileConfiguration.CodeBlockType.Value
}
}
codeResult.append(translation)
}
CompileConfiguration.CodeBlockType.Reference -> {
if (isFirstReference) {
codeResult.append(translation)
isFirstReference = false
} else {
referenceResult.append(translation)
}
}
}
}
else -> if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Note) {
codeResult.append(translation)
} else if (compileConfiguration.codeBlockType == CompileConfiguration.CodeBlockType.Reference) {
//资源引用值应该被整体处理
referenceResult.append(translation)
} else {
if (translation.startsWith("#") && compileConfiguration.codeBlockType ==
CompileConfiguration.CodeBlockType.Key
@ -430,7 +480,7 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
if (!tag.isNullOrBlank()) {
//如果此类型为特殊标注,那么设置为注释
compileConfiguration.codeBlockType =
CompileConfiguration.CodeBlockType.Note
CompileConfiguration.CodeBlockType.Reference
}
}
codeResult.append(codeInfo.code)
@ -465,15 +515,21 @@ class CodeCompiler2 private constructor(val context: Context) : CodeCompilerInte
}
}
/**
* 解析行数据
* 检查行数据此函数不修改结果集
* @param compileConfiguration CompileConfiguration 代码配置
* @param compilerListener CodeCompilerListener? 代码监听器
*/
fun analysisLineCode(
fun checkLineCode(
compileConfiguration: CompileConfiguration,
compilerListener: CodeCompilerListener? = null
) {
DebugHelper.printLog(
debugKey,
"键[" + compileConfiguration.getKey() + "]值[" + compileConfiguration.getValue() + "]",
"行处理"
)
compileConfiguration.setCanAddError(true)
val key = compileConfiguration.getKey()
//设置了监听器并且key不为空

View File

@ -7,6 +7,7 @@ import android.text.SpannableString
import android.text.Spanned
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.View
import com.coldmint.rust.core.*
import java.io.File
@ -40,6 +41,18 @@ data class CompileConfiguration(
) {
/**
* 增加值
* @param string String
*/
fun appendValue(string: String) {
if (codeBlockType == CodeBlockType.Reference) {
valueBuilder.append(string)
} else {
Log.e(CodeCompiler2.debugKey, "只能在引用语句块设置值。")
}
}
/**
* 设置是否可以添加错误只能在特定的函数内添加错误
* @param canAddError Boolean

View File

@ -1,19 +1,35 @@
package com.coldmint.rust.core.dataBean.dataset
import com.google.gson.annotations.SerializedName
data class DataBaseManifest(
val id: String,
@SerializedName("author")
val author: String,
@SerializedName("describe")
val describe: String,
@SerializedName("id")
val id: String,
@SerializedName("name")
val name: String,
val versionNumber: Int,
@SerializedName("tables")
val tables: Tables,
@SerializedName("updateLog")
val updateLog: List<String>,
@SerializedName("versionName")
val versionName: String,
val tables: Tables
@SerializedName("versionNumber")
val versionNumber: Int
) {
data class Tables(
val chain_inspection: String,
@SerializedName("chain_inspection")
val chainInspection: String,
@SerializedName("code")
val code: String,
val game_version: String,
@SerializedName("game_version")
val gameVersion: String,
@SerializedName("section")
val section: String,
val value_type: String
@SerializedName("value_type")
val valueType: String
)
}

View File

@ -37,8 +37,24 @@ interface CodeDao {
@Query("SELECT * FROM code WHERE translate LIKE '%'||:key||'%' AND (section=:section OR section='all') LIMIT :limitNum")
fun findCodeByKeyInSection(key: String, section: String, limitNum: Int): List<CodeInfo>?
/**
* 在类型内通过翻译查找代码
* @param key String
* @param type String
* @param limitNum Int
* @return List<CodeInfo>?
*/
@Query("SELECT * FROM code WHERE translate LIKE '%'||:key||'%' AND type=:type LIMIT :limitNum")
fun findCodeByKeyInType(key: String, type: String, limitNum: Int): List<CodeInfo>?
fun findCodeByTranslateInType(key: String, type: String, limitNum: Int): List<CodeInfo>?
/**
* 在类型内通过Code查找代码
* @param key String
* @param type String
* @param limitNum Int
* @return List<CodeInfo>?
*/
@Query("SELECT * FROM code WHERE code LIKE '%'||:key||'%' AND type=:type LIMIT :limitNum")
fun findCodeByCodeInType(key: String, type: String, limitNum: Int): List<CodeInfo>?
/**
* 搜索代码

View File

@ -9,12 +9,8 @@ import androidx.room.RoomDatabase
import com.coldmint.rust.core.DataSet
import com.coldmint.rust.core.dataBean.dataset.*
import com.coldmint.rust.core.database.file.FileDataBase
import com.coldmint.rust.core.database.file.FileTable
import com.coldmint.rust.core.database.file.HistoryRecord
import com.coldmint.rust.core.database.file.ValueTable
import com.coldmint.rust.core.tool.FileOperator
import com.google.gson.Gson
import java.io.File
import java.util.concurrent.Executors
/**
@ -226,7 +222,7 @@ abstract class CodeDataBase : RoomDatabase() {
try {
Log.d("数据集加载", "加载值表...")
val valueTypeData =
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.value_type))
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.valueType))
if (valueTypeData != null) {
val valueTypeDataBean =
gson.fromJson<ValueTypeDataBean>(
@ -286,7 +282,7 @@ abstract class CodeDataBase : RoomDatabase() {
try {
Log.d(datasetTag, "加载链式检查表...")
val chainInspectionData =
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.chain_inspection))
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.chainInspection))
if (chainInspectionData != null) {
val chainInspectionDataBean = gson.fromJson<ChainInspectionDataBean>(
chainInspectionData,
@ -345,7 +341,7 @@ abstract class CodeDataBase : RoomDatabase() {
try {
Log.d("数据集加载", "加载版本表...")
val versionData =
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.game_version))
FileOperator.readFile(dataSet.getAbsolutePath(manifest.tables.gameVersion))
if (versionData != null) {
val versionDataBean =
gson.fromJson<GameVersionDataBean>(

View File

@ -27,6 +27,7 @@ interface CodeCompilerListener {
* @param code String 代码
* @param section String
*/
fun onClickKeyNotFoundItem(
lineNum: Int,
columnNum: Int,
@ -42,6 +43,7 @@ interface CodeCompilerListener {
* @param view View 视图
* @param valueType ValueTypeInfo 值类型
*/
fun onClickValueTypeErrorItem(
lineNum: Int,
columnNum: Int,
@ -57,6 +59,7 @@ interface CodeCompilerListener {
* @param view View
* @param sectionName String
*/
fun onClickSectionIndexError(lineNum: Int, columnNum: Int, view: View, sectionName: String)
/**
@ -92,6 +95,7 @@ interface CodeCompilerListener {
* @param sectionName String 节名
* @param needName Boolean 是否需要附加名
*/
fun onClickSectionNameErrorItem(
lineNum: Int,
columnNum: Int,
@ -107,6 +111,7 @@ interface CodeCompilerListener {
* @param view View 视图
* @param sectionName String 节名
*/
fun onClickCodeIndexErrorItem(lineNum: Int, view: View, sectionName: String)
/**

View File

@ -0,0 +1,31 @@
package com.coldmint.rust.core.tool
import android.util.Log
import com.coldmint.rust.core.CodeCompiler2
/**
* 调试帮助器
*/
object DebugHelper {
/**
* 日志打印
* @param message String 消息
* @param module String? 模块名
* @param isError Boolean 是否为错误
*/
fun printLog(keyValue: String, message: String, module: String? = null, isError: Boolean = false) {
val key = if (module == null) {
keyValue
} else {
"$keyValue-$module"
}
if (isError) {
Log.e(key, message)
} else {
Log.d(key, message)
}
}
}

View File

@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sat Apr 23 18:41:07 CST 2022
sdk.dir=D\:\\Android_SDK
#Sun Apr 24 15:32:29 CST 2022
sdk.dir=D\:\\Document\\AndroidSdk