Skip to content

Commit

Permalink
add: newLine option to helper
Browse files Browse the repository at this point in the history
This commit adds a new option to the helper which removes the unecessary \n between 2 definitions.
  • Loading branch information
ThePedroo committed Oct 11, 2023
1 parent cebae49 commit a4de30c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 157 deletions.
4 changes: 3 additions & 1 deletion OS_SUPPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ The optimizations are the options that the coder can use to optimize the VN code

##### Version support

\>= `v1.22.0 & v1.20.0`: Supported
\>= `v1.22.0 & v1.20.0`: Supported without `preCalculateScenesInfo` & `hashAchievementIds`

\>= `v1.23.0 & v1.21.0`: Supported

### Scenes

Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

- (Customizability) Allow to remove footer and add buttons manually.
- (Code quality) Only add necessary `import`s.
- (Code quality) `multiDefinitions` helper for avoiding the \n between 2 resource imports.
- (Optimization) Create functions for stopping the music and sound effects.
3 changes: 0 additions & 3 deletions android/app/src/main/java/com/perforvnm/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ class MainActivity : Activity() {
)

val sdp88 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._88sdp)

val sdpminus3 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._minus3sdp)

layoutParamsStart.gravity = Gravity.BOTTOM or Gravity.START
Expand Down Expand Up @@ -722,7 +721,6 @@ class MainActivity : Activity() {
)

val sdp135 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._135sdp)

val sdp77 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._77sdp)

layoutParamsSeekBar.gravity = Gravity.TOP or Gravity.START
Expand Down Expand Up @@ -1179,7 +1177,6 @@ class MainActivity : Activity() {
}

val sdp100 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._100sdp)

val sdp70 = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._70sdp)

val layoutParamsSavesBackground = LayoutParams(
Expand Down
10 changes: 4 additions & 6 deletions src/coder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fs from 'fs'

import helper from './helper.js'
import finalizer from './finalizer.js'

import achievements from './achievements.js'
import { nextTick } from 'process'

global.visualNovel = { menu: null, info: null, internalInfo: {}, code: '', scenes: [], subScenes: [], achievements: [], customXML: [], optimizations: {} }
global.PerforVNM = {
Expand Down Expand Up @@ -174,13 +172,13 @@ function finalize() {
when (scene) {`, 2
)

let savesSwitchCode = finalizer.sceneEachInit()
let savesSwitchCode = ''

if (visualNovel.scenes.length) {
let scenesCode = ''

visualNovel.scenes.forEach((scene, i) => {
savesSwitchCode += finalizer.sceneEach(scene)
savesSwitchCode += helper.sceneEach(scene)

if (i != visualNovel.scenes.length - 1) {
const nextScene = visualNovel.scenes[i + 1]
Expand Down Expand Up @@ -419,7 +417,7 @@ ${finishScene.join('\n')}\n\n`, 8, 0)
})

visualNovel.subScenes.forEach((scene, i) => {
savesSwitchCode += finalizer.sceneEach(scene)
savesSwitchCode += helper.sceneEach(scene)

if (scene.next) {
const nextScene = visualNovel.scenes.find((nScene) => nScene.name == scene.next)
Expand Down Expand Up @@ -650,7 +648,7 @@ ${finishScene.join('\n')}\n\n`, 6, 0)
helper.replace('__PERFORVNM_SAVES_SWITCH__', defaultSaveSwitchCode)
helper.replace('__PERFORVNM_HEADERS__', '\nimport kotlin.math.roundToInt')
} else {
helper.replace('__PERFORVNM_SAVES_SWITCH__', finalizer.sceneEachFinalize(savesSwitchCode))
helper.replace('__PERFORVNM_SAVES_SWITCH__', helper.sceneEachFinalize(savesSwitchCode))
}

helper.replace('__PERFORVNM_HEADERS__', '')
Expand Down
103 changes: 0 additions & 103 deletions src/finalizer.js

This file was deleted.

124 changes: 105 additions & 19 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,14 @@ function addResource(page, resource) {
return page
}

function addMultipleResources(page, page2, resource) {
addResource(page, resource)
addResource(page2, resource)
}

function getResource(page, resource, add) {
function getResource(page, resource) {
const cResource = page.resources.find((cResource) => resource.dp == cResource.dp && resource.type == cResource.type)

if (resource.type == 'sdp') {
if (!visualNovel.optimizations.reuseResources) return {
definition: null,
inlined: `resources.getDimensionPixelSize(resources.getIdentifier("_${resource.dp}${resource.type}", "dimen", getPackageName()))`,
variable: `resources.getDimensionPixelSize(resources.getIdentifier("_${resource.dp}${resource.type}", "dimen", getPackageName()))`,
inlined: `resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${resource.dp}${resource.type})`,
variable: `resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${resource.dp}${resource.type})`,
additionalSpace: '\n'
}

Expand All @@ -237,8 +232,6 @@ function getResource(page, resource, add) {
additionalSpace: '\n'
}

if (add) console.log(page.resources)

if (cResource) return {
definition: null,
inlined: `__PERFORVNM_${resource.dp}_${resource.type}_INLINE__`,
Expand All @@ -256,13 +249,11 @@ function getResource(page, resource, add) {
}
}

function getMultipleResources(page, page2, resource, add) {
function getMultipleResources(page, page2, resource) {
let cResource = null

cResource = getResource(page, resource, add)
if (add) console.log(cResource)
if (cResource.definition) cResource = getResource(page2, resource, add)
if (add) console.log(cResource)
cResource = getResource(page, resource)
if (cResource.definition) cResource = getResource(page2, resource)

return cResource
}
Expand Down Expand Up @@ -291,11 +282,11 @@ function finalizeResources(page, code) {
}

if (resource.type == 'sdp') {
code = code.replace(defineRegex, `val ${resource.type}${resource.dp} = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${resource.dp}${resource.type})\n\n${spaces}`)
code = code.replace(defineRegex, `val ${resource.type}${resource.dp} = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${resource.dp}${resource.type})${resource.newLines ? resource.newLines : '\n\n'}${spaces}`)
code = code.replace(inlineRegex, `resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${resource.dp}${resource.type})`)
code = code.replace(variableRegex, `${resource.type}${resource.dp}`)
} else {
code = code.replace(defineRegex, `val ${resource.type}${resource.dp} = resources.getDimension(com.intuit.ssp.R.dimen._${resource.dp}${resource.type})\n\n${spaces}`)
code = code.replace(defineRegex, `val ${resource.type}${resource.dp} = resources.getDimension(com.intuit.ssp.R.dimen._${resource.dp}${resource.type})${resource.newLines ? resource.newLines : '\n\n'}${spaces}`)
code = code.replace(inlineRegex, `resources.getDimension(com.intuit.ssp.R.dimen._${resource.dp}${resource.type})`)
code = code.replace(variableRegex, `${resource.type}${resource.dp}`)
}
Expand Down Expand Up @@ -341,6 +332,100 @@ function removeAllDoubleLines(code) {
}
}


function sceneEach(scene) {
let savesSwitchLocal = helper.codePrepare(`
${helper.getSceneId(scene.name)} -> {
when (characterData.getString("name")) {`, 0, 6, false
)

scene.characters.forEach((character) => {
let optimizedSetImage = ''
if (visualNovel.optimizations.preCalculateScenesInfo) {
optimizedSetImage = helper.codePrepare(`
imageViewCharacter.setImageResource(R.raw.${character.image})\n\n `, 8
)
}
switch (character.position.side) {
case 'left': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}val leftDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.side * 0.25)}sdp)
layoutParamsImageViewCharacter.setMargins(leftDpLoad + leftDpCharacter, topDpLoad, 0, 0)
}`, 0, 4, false
)

break
}
case 'leftTop': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}val leftDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.side * 0.25)}sdp)
val topDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.top * 0.25)}sdp)
layoutParamsImageViewCharacter.setMargins(leftDpLoad + leftDpCharacter, topDpLoad + topDpCharacter, 0, 0)
}`, 0, 4, false
)

break
}
case 'right': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}val rightDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.side * 0.25)}sdp)
layoutParamsImageViewCharacter.setMargins(leftDpLoad - rightDpCharacter, topDpLoad, 0, 0)
}`, 0, 4, false
)

break
}
case 'rightTop': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}val rightDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.side * 0.25)}sdp)
val topDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.top * 0.25)}sdp)
layoutParamsImageViewCharacter.setMargins(leftDpLoad - rightDpCharacter, topDpLoad + topDpCharacter, 0, 0)
}`, 0, 4, false
)

break
}
case 'top': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}val topDpCharacter = resources.getDimensionPixelSize(com.intuit.sdp.R.dimen._${Math.round(character.position.margins.side * 0.25)}sdp)
layoutParamsImageViewCharacter.setMargins(leftDpLoad, topDpLoad + topDpCharacter, 0, 0)
}`, 0, 4, false
)

break
}
case 'center': {
savesSwitchLocal += helper.codePrepare(`
"${character.name}" -> {
${optimizedSetImage}layoutParamsImageViewCharacter.setMargins(leftDpLoad, topDpLoad, 0, 0)
}`, 0, 4, false
)

break
}
}
})

return savesSwitchLocal + '\n' + helper.codePrepare('}\n', 0, 12, false) + helper.codePrepare('}', 0, 10, false)
}

function sceneEachFinalize(savesSwitchCode) {
return helper.codePrepare(`
when (buttonData.get${visualNovel.optimizations.hashScenesNames ? 'Int' : 'String'}("scene")) {`, 0, 4
) + savesSwitchCode + '\n' +
helper.codePrepare('}', 0, 8, false)
}

export default {
writeFunction,
replace,
Expand All @@ -352,13 +437,14 @@ export default {
verifyParams,
codePrepare,
addResource,
addMultipleResources,
getResource,
getMultipleResources,
finalizeResources,
finalizeMultipleResources,
hash,
getSceneId,
getAchievementId,
removeAllDoubleLines
removeAllDoubleLines,
sceneEach,
sceneEachFinalize
}
Loading

0 comments on commit a4de30c

Please sign in to comment.