@@ -64,6 +64,7 @@ import com.google.android.material.snackbar.Snackbar
6464import com.mokkachocolata.library.pcsimsaveeditor.MainFunctions
6565import org.json.JSONArray
6666import org.json.JSONObject
67+ import org.luaj.vm2.Globals
6768import org.luaj.vm2.LuaValue
6869import org.luaj.vm2.lib.OneArgFunction
6970import org.luaj.vm2.lib.ThreeArgFunction
@@ -126,6 +127,18 @@ class MainActivity2 : AppCompatActivity() {
126127 val version : String? ,
127128 val source : String
128129 )
130+ var lua_openFile_dat : Uri ? = null
131+ var lua_openFile_picked = false
132+ lateinit var lua_global : Globals
133+
134+ val result = registerForActivityResult(ActivityResultContracts .OpenDocument ()) {data ->
135+ if (data != null ) {
136+ lua_openFile_dat = data
137+ lua_openFile_picked = true
138+ } else {
139+ lua_openFile_picked = false
140+ }
141+ }
129142 data class Apps (val name : String , val path : String )
130143 private var fileList = arrayOf(
131144 Apps (" App Manager" , " App Downloader.exe" ),
@@ -148,28 +161,22 @@ class MainActivity2 : AppCompatActivity() {
148161 Apps (" Boot File" , " System/boot.bin" ),
149162 Apps (" Virus" , " Launcher.exe" )
150163 )
151- private class PCSimulatorSaveEditorUtilClass () : TwoArgFunction() {
164+ private class PCSimulatorSaveEditorUtilClass : TwoArgFunction () {
152165 lateinit var globalVars : GlobalVars
153166 lateinit var activity2: MainActivity2
154167 lateinit var edittext: EditText
155168 lateinit var menu: Menu
156169 override fun call (modname : LuaValue ? , env : LuaValue ? ): LuaValue {
157170 val library = tableOf()
158- val getVer = getVersion()
159- getVer.globalVars = globalVars
160- library.set(" GetVersion" , getVer)
161- library.set(" GetPlatform" , object : ZeroArgFunction () {
162- override fun call (): LuaValue {
163- return LuaValue .valueOf(0 )
164- }
165- })
171+ library[" Version" ] = LuaValue .valueOf(globalVars.version)
172+ library[" Platform" ] = valueOf(0 )
166173 library.set(" Print" , object : OneArgFunction () {
167174 override fun call (arg : LuaValue ? ): LuaValue {
168175 arg?.toString()?.let { Log .i(" Script" , it) }
169176 return NONE
170177 }
171178 })
172- library.set( " DisplayDialog" , object : TwoArgFunction () {
179+ library[ " DisplayDialog" ] = object : TwoArgFunction () {
173180 override fun call (arg1 : LuaValue ? , arg2 : LuaValue ? ): LuaValue {
174181 if (Build .VERSION .SDK_INT >= 31 ) {
175182 val builder = MaterialAlertDialogBuilder (activity2)
@@ -186,24 +193,24 @@ class MainActivity2 : AppCompatActivity() {
186193 }
187194 return NONE
188195 }
189- })
190- library.set( " DecryptString" , object : OneArgFunction () {
196+ }
197+ library[ " DecryptString" ] = object : OneArgFunction () {
191198 override fun call (arg1 : LuaValue ? ): LuaValue {
192199 return LuaValue .valueOf(MainFunctions ().Decrypt (arg1?.toString()))
193200 }
194- })
195- library.set( " SetSaveContents" , object : OneArgFunction () {
201+ }
202+ library[ " SetSaveContents" ] = object : OneArgFunction () {
196203 override fun call (contents : LuaValue ? ): LuaValue {
197204 edittext.setText(contents?.toString())
198205 return NONE
199206 }
200- })
201- library.set( " GetSaveContents" , object : ZeroArgFunction () {
207+ }
208+ library[ " GetSaveContents" ] = object : ZeroArgFunction () {
202209 override fun call (): LuaValue {
203210 return LuaValue .valueOf(edittext.text.toString())
204211 }
205- })
206- library.set( " AddMenuItem" , object : TwoArgFunction () {
212+ }
213+ library[ " AddMenuItem" ] = object : TwoArgFunction () {
207214 override fun call (name : LuaValue ? , callback : LuaValue ? ): LuaValue {
208215 val item = menu.add(name?.toString())
209216 item.setOnMenuItemClickListener {
@@ -219,8 +226,24 @@ class MainActivity2 : AppCompatActivity() {
219226 })
220227 return delFunction
221228 }
222- })
223- library.set(" DisplayEditTextDialog" , object : ThreeArgFunction () {
229+ }
230+ library[" OpenFile" ] = object : OneArgFunction () {
231+ override fun call (memeType : LuaValue ? ): LuaValue {
232+ activity2.lua_openFile_dat = null
233+ activity2.lua_openFile_picked = false
234+ if (memeType != null ) activity2.result.launch(arrayOf(memeType.toString()))
235+ else activity2.result.launch(arrayOf(" */*" ))
236+
237+ val table = tableOf()
238+ if (activity2.lua_openFile_dat != null )
239+ table.set(" Text" , LuaValue .valueOf(activity2.readTextFromUri(activity2.lua_openFile_dat!! )))
240+ else
241+ table.set(" Text" , " " )
242+ table.set(" Picked" , LuaValue .valueOf(activity2.lua_openFile_picked))
243+ return table
244+ }
245+ }
246+ library[" DisplayEditTextDialog" ] = object : ThreeArgFunction () {
224247 override fun call (arg1 : LuaValue ? , message : LuaValue ? , callback : LuaValue ? ): LuaValue {
225248 val edittext = EditText (activity2)
226249 if (Build .VERSION .SDK_INT >= 31 ) {
@@ -236,7 +259,7 @@ class MainActivity2 : AppCompatActivity() {
236259 }
237260 builder.show()
238261 } else {
239- val builder: AlertDialog . Builder = AlertDialog .Builder (activity2)
262+ val builder = AlertDialog .Builder (activity2)
240263 builder.setIcon(R .drawable.baseline_info_outline_24).setTitle(arg1?.toString())
241264 builder.setView(edittext)
242265 builder.setMessage(message?.toString())
@@ -250,8 +273,8 @@ class MainActivity2 : AppCompatActivity() {
250273 }
251274 return NONE
252275 }
253- })
254- library.set( " DisplayCheckboxDialog" , object : ThreeArgFunction () {
276+ }
277+ library[ " DisplayCheckboxDialog" ] = object : ThreeArgFunction () {
255278 override fun call (arg1 : LuaValue ? , message : LuaValue ? , callback : LuaValue ? ): LuaValue {
256279 if (Build .VERSION .SDK_INT >= 31 ) {
257280 val edittext = MaterialSwitch (activity2)
@@ -282,17 +305,16 @@ class MainActivity2 : AppCompatActivity() {
282305 }
283306 return NONE
284307 }
285- })
286- library.set( " DisplayListDialog" , object : ThreeArgFunction () {
308+ }
309+ library[ " DisplayListDialog" ] = object : ThreeArgFunction () {
287310 override fun call (arg1 : LuaValue ? , list : LuaValue ? , callback : LuaValue ? ): LuaValue {
288311 if (Build .VERSION .SDK_INT >= 31 ) {
289312 val builder = MaterialAlertDialogBuilder (activity2)
290313 builder.setIcon(R .drawable.baseline_info_outline_24).setTitle(arg1?.toString())
291314 val arr = arrayListOf<String >()
292315 if (list?.istable() == true ) {
293- for (i in 0 .. list.length()) {
316+ for (i in 0 .. list.length())
294317 if (list[i].isstring()) arr.add(list[i].toString())
295- }
296318 builder.setItems(arr.toTypedArray()) {_, i ->
297319 callback?.call(LuaValue .valueOf(i))
298320 }
@@ -315,8 +337,8 @@ class MainActivity2 : AppCompatActivity() {
315337 return NONE
316338 }
317339
318- })
319- library.set( " DisplayMultiChoiceListDialog" , object : ThreeArgFunction () {
340+ }
341+ library[ " DisplayMultiChoiceListDialog" ] = object : ThreeArgFunction () {
320342 override fun call (arg1 : LuaValue ? , list : LuaValue ? , callback : LuaValue ? ): LuaValue {
321343 if (Build .VERSION .SDK_INT >= 31 ) {
322344 val builder = MaterialAlertDialogBuilder (activity2)
@@ -366,31 +388,22 @@ class MainActivity2 : AppCompatActivity() {
366388 }
367389 return NONE
368390 }
369-
370- })
371- library.set(" OpenURL" , object : OneArgFunction () {
391+ }
392+ library[" OpenURL" ] = object : OneArgFunction () {
372393 override fun call (uri : LuaValue ? ): LuaValue {
373394 activity2.startActivity(Intent (Intent .ACTION_VIEW , Uri .parse(uri?.toString())))
374395 return NONE
375396 }
376-
377- })
378- library.set(" PLATFORM_ANDROID" , 0 )
379- library.set(" PLATFORM_JAVA" , 1 )
380- library.set(" PLATFORM_UNITY" , 2 )
381- library.set(" DIALOG_BUTTON_OK" , 0 )
382- library.set(" DIALOG_BUTTON_CANCEL" , 1 )
397+ }
398+ library[" PLATFORM_ANDROID" ] = 0
399+ library[" PLATFORM_JAVA" ] = 1
400+ library[" PLATFORM_UNITY" ] = 2
401+ library[" DIALOG_BUTTON_OK" ] = 0
402+ library[" DIALOG_BUTTON_CANCEL" ] = 1
383403 env?.set(" SaveEditor" , library)
384404 return library
385405 }
386406
387- class getVersion () : ZeroArgFunction() {
388- lateinit var globalVars : GlobalVars
389- override fun call (): LuaValue {
390- return LuaValue .valueOf(globalVars.version)
391- }
392- }
393-
394407 }
395408 private fun doOnThread (obj : Runnable , wait : Boolean ) {
396409 val actualThread = Thread (obj)
@@ -417,9 +430,7 @@ class MainActivity2 : AppCompatActivity() {
417430 )
418431 val bm = BitmapFactory .decodeStream(resolver.openInputStream(uri))
419432 val baos = ByteArrayOutputStream ()
420- Thread {
421- bm.compress(Bitmap .CompressFormat .JPEG , 100 , baos)
422- }.apply {
433+ Thread { bm.compress(Bitmap .CompressFormat .JPEG , 100 , baos) }.apply {
423434 start()
424435 join()
425436 }
@@ -717,7 +728,8 @@ class MainActivity2 : AppCompatActivity() {
717728 |Get beta builds at the Actions tab at the GitHub repository.
718729 |Report any issues at the Issues tab at the GitHub repository.
719730 |This app is licensed with GPLv3.0.
720- |""" .trimMargin(), {_,_-> }, null
731+ |This project is neither associated, affiliated, nor endorsed by Intel or AMD.
732+ |""" .trimMargin(), {_,_-> }, null // This way we won't get into trouble
721733 )
722734 R .id.help -> {
723735 System .gc()
@@ -1160,8 +1172,9 @@ class WriteOrReadThread : Runnable{
11601172 doOnThread(functions)
11611173 clazz.saveString = functions.Output
11621174 clazz.saveTheFile.launch(" Save.txt" )
1163- } }
1175+ }
11641176 }
1177+ }
11651178
11661179class AfterReadThread : Runnable {
11671180 lateinit var afterData : Uri
@@ -1170,13 +1183,13 @@ class AfterReadThread : Runnable{
11701183
11711184 override fun run () {
11721185 try {
1173- resolver.openFileDescriptor(afterData, " w" )?.use { it ->
1174- val outputstream = FileOutputStream (it.fileDescriptor)
1175- outputstream.use { it.write(text.toByteArray()) }
1176- outputstream.close()
1177- }
1186+ resolver.openFileDescriptor(afterData, " w" )?.use { it ->
1187+ val outputstream = FileOutputStream (it.fileDescriptor)
1188+ outputstream.use { it.write(text.toByteArray()) }
1189+ outputstream.close()
1190+ }
11781191 } catch (e: Exception ) {
1179- e.printStackTrace()
1192+ e.printStackTrace()
11801193 }
11811194 }
11821195}
0 commit comments