Skip to content

Commit

Permalink
Added drag/drop functionality to the add mods button.
Browse files Browse the repository at this point in the history
You can now drag one or more files onto the add mods button in order to add them to the mods directory. Only jar, litemod, and zip files will be moved.
Changed eslint to use a single configuration file, with overrides for the UI scripts.
Now using fs-extra, replace usages of rimraf and mkdirp with fs-extra functions.
  • Loading branch information
dscalzi committed Dec 1, 2018
1 parent d9c9b32 commit d779eac
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 143 deletions.
15 changes: 14 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@
"argsIgnorePattern": "reject"
}
]
}
},
"overrides": [
{
"files": [ "app/assets/js/scripts/*.js" ],
"rules": {
"no-unused-vars": [
0
],
"no-undef": [
0
]
}
}
]
}
47 changes: 0 additions & 47 deletions .eslintrc.scripts.json

This file was deleted.

5 changes: 3 additions & 2 deletions app/assets/css/launcher.css
Original file line number Diff line number Diff line change
Expand Up @@ -1545,13 +1545,15 @@ input:checked + .toggleSwitchSlider:before {
margin-bottom: 10px;
}
#settingsDropinFileSystemButton:hover,
#settingsDropinFileSystemButton:focus {
#settingsDropinFileSystemButton:focus,
#settingsDropinFileSystemButton[drag] {
background: rgba(54, 54, 54, 0.25);
text-shadow: 0px 0px 20px white;
}
/* Refresh instructions on the file system button. */
#settingsDropinRefreshNote {
font-size: 10px;
pointer-events: none;
}

/* Button to remove drop-in mods. */
Expand Down Expand Up @@ -2663,7 +2665,6 @@ input:checked + .toggleSwitchSlider:before {
box-shadow: 0px 0px 10px 0px rgb(0, 0, 0);
overflow: hidden;
position: relative;
/*background-image: url('https://cdn.discordapp.com/avatars/169197209630277642/6650b5a50e1cb3d00a79b9b88b9a0cd4.png');*/
background-position: center;
background-repeat: no-repeat;
background-size: contain;
Expand Down
11 changes: 5 additions & 6 deletions app/assets/js/assetguard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const async = require('async')
const child_process = require('child_process')
const crypto = require('crypto')
const EventEmitter = require('events')
const fs = require('fs')
const mkpath = require('mkdirp')
const fs = require('fs-extra')
const path = require('path')
const Registry = require('winreg')
const request = require('request')
Expand Down Expand Up @@ -360,7 +359,7 @@ class AssetGuard extends EventEmitter {
const versionPath = path.join(commonPath, 'versions', forgeVersion.id)
const versionFile = path.join(versionPath, forgeVersion.id + '.json')
if(!fs.existsSync(versionFile)){
mkpath.sync(versionPath)
fs.ensureDirSync(versionPath)
fs.writeFileSync(path.join(versionPath, forgeVersion.id + '.json'), zipEntries[i].getData())
resolve(forgeVersion)
} else {
Expand Down Expand Up @@ -996,7 +995,7 @@ class AssetGuard extends EventEmitter {
const url = await self._getVersionDataUrl(version)
//This download will never be tracked as it's essential and trivial.
console.log('Preparing download of ' + version + ' assets.')
mkpath.sync(versionPath)
fs.ensureDirSync(versionPath)
const stream = request(url).pipe(fs.createWriteStream(versionFile))
stream.on('finish', () => {
resolve(JSON.parse(fs.readFileSync(versionFile)))
Expand Down Expand Up @@ -1078,7 +1077,7 @@ class AssetGuard extends EventEmitter {
let data = null
if(!fs.existsSync(assetIndexLoc) || force){
console.log('Downloading ' + versionData.id + ' asset index.')
mkpath.sync(indexPath)
fs.ensureDirSync(indexPath)
const stream = request(assetIndex.url).pipe(fs.createWriteStream(assetIndexLoc))
stream.on('finish', () => {
data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
Expand Down Expand Up @@ -1457,7 +1456,7 @@ class AssetGuard extends EventEmitter {

async.eachLimit(dlQueue, limit, (asset, cb) => {

mkpath.sync(path.join(asset.to, '..'))
fs.ensureDirSync(path.join(asset.to, '..'))

let req = request(asset.from)
req.pause()
Expand Down
7 changes: 3 additions & 4 deletions app/assets/js/configmanager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs')
const mkpath = require('mkdirp')
const fs = require('fs-extra')
const os = require('os')
const path = require('path')

Expand Down Expand Up @@ -98,7 +97,7 @@ exports.load = function(){

if(!fs.existsSync(filePath)){
// Create all parent directories.
mkpath.sync(path.join(filePath, '..'))
fs.ensureDirSync(path.join(filePath, '..'))
config = DEFAULT_CONFIG
exports.save()
} else {
Expand All @@ -110,7 +109,7 @@ exports.load = function(){
logger.error(err)
logger.log('Configuration file contains malformed JSON or is corrupt.')
logger.log('Generating a new configuration file.')
mkpath.sync(path.join(filePath, '..'))
fs.ensureDirSync(path.join(filePath, '..'))
config = DEFAULT_CONFIG
exports.save()
}
Expand Down
23 changes: 18 additions & 5 deletions app/assets/js/dropinmodutil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs')
const mkpath = require('mkdirp')
const fs = require('fs-extra')
const path = require('path')
const { shell } = require('electron')

Expand All @@ -16,9 +15,7 @@ const DISABLED_EXT = '.disabled'
* @param {string} modsDir The path to the mods directory.
*/
exports.validateModsDir = function(modsDir) {
if(!fs.existsSync(modsDir)) {
mkpath.sync(modsDir)
}
fs.ensureDirSync(modsDir)
}

/**
Expand Down Expand Up @@ -66,6 +63,22 @@ exports.scanForDropinMods = function(modsDir, version) {
return modsDiscovered
}

/**
* Add dropin mods.
*
* @param {FileList} files The files to add.
* @param {string} modsDir The path to the mods directory.
*/
exports.addDropinMods = function(files, modsdir) {

for(let f of files) {
if(MOD_REGEX.exec(f.name) != null) {
fs.moveSync(f.path, path.join(modsdir, f.name))
}
}

}

/**
* Delete a drop-in mod from the file system.
*
Expand Down
4 changes: 2 additions & 2 deletions app/assets/js/preloader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {ipcRenderer} = require('electron')
const fs = require('fs-extra')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')

const ConfigManager = require('./configmanager')
const DistroManager = require('./distromanager')
Expand Down Expand Up @@ -56,7 +56,7 @@ DistroManager.pullRemote().then((data) => {
})

// Clean up temp dir incase previous launches ended unexpectedly.
rimraf(path.join(os.tmpdir(), ConfigManager.getTempNativeFolder()), (err) => {
fs.remove(path.join(os.tmpdir(), ConfigManager.getTempNativeFolder()), (err) => {
if(err){
logger.warn('Error while cleaning natives directory', err)
} else {
Expand Down
10 changes: 4 additions & 6 deletions app/assets/js/processbuilder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const AdmZip = require('adm-zip')
const child_process = require('child_process')
const crypto = require('crypto')
const fs = require('fs')
const mkpath = require('mkdirp')
const fs = require('fs-extra')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const {URL} = require('url')

const { Library } = require('./assetguard')
Expand Down Expand Up @@ -36,7 +34,7 @@ class ProcessBuilder {
* Convienence method to run the functions typically used to build a process.
*/
build(){
mkpath.sync(this.gameDir)
fs.ensureDirSync(this.gameDir)
const tempNativePath = path.join(os.tmpdir(), ConfigManager.getTempNativeFolder(), crypto.pseudoRandomBytes(16).toString('hex'))
process.throwDeprecation = true
this.setupLiteLoader()
Expand Down Expand Up @@ -74,7 +72,7 @@ class ProcessBuilder {
})
child.on('close', (code, signal) => {
logger.log('Exited with code', code)
rimraf(tempNativePath, (err) => {
fs.remove(tempNativePath, (err) => {
if(err){
logger.warn('Error while deleting temp dir', err)
} else {
Expand Down Expand Up @@ -364,7 +362,7 @@ class ProcessBuilder {
const libs = []

const libArr = this.versionData.libraries
mkpath.sync(tempNativePath)
fs.ensureDirSync(tempNativePath)
for(let i=0; i<libArr.length; i++){
const lib = libArr[i]
if(Library.validateRules(lib.rules)){
Expand Down
31 changes: 27 additions & 4 deletions app/assets/js/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,25 @@ function bindDropinModFileSystemButton(){
DropinModUtil.validateModsDir(CACHE_SETTINGS_MODS_DIR)
shell.openItem(CACHE_SETTINGS_MODS_DIR)
}
fsBtn.ondragenter = e => {
e.dataTransfer.dropEffect = 'move'
fsBtn.setAttribute('drag', '')
e.preventDefault()
}
fsBtn.ondragover = e => {
e.preventDefault()
}
fsBtn.ondragleave = e => {
fsBtn.removeAttribute('drag')
}

fsBtn.ondrop = e => {
fsBtn.removeAttribute('drag')
e.preventDefault()

DropinModUtil.addDropinMods(e.dataTransfer.files, CACHE_SETTINGS_MODS_DIR)
reloadDropinMods()
}
}

/**
Expand Down Expand Up @@ -676,14 +695,18 @@ function saveDropinModConfiguration(){
document.addEventListener('keydown', (e) => {
if(getCurrentView() === VIEWS.settings && selectedSettingsTab === 'settingsTabMods'){
if(e.key === 'F5'){
resolveDropinModsForUI()
bindDropinModsRemoveButton()
bindDropinModFileSystemButton()
bindModsToggleSwitch()
reloadDropinMods()
}
}
})

function reloadDropinMods(){
resolveDropinModsForUI()
bindDropinModsRemoveButton()
bindDropinModFileSystemButton()
bindModsToggleSwitch()
}

// Server status bar functions.

/**
Expand Down
Loading

0 comments on commit d779eac

Please sign in to comment.