added possibility to load ModPacks

This commit is contained in:
knoxfighter 2017-09-07 23:35:27 +09:00
parent 9980ead1b7
commit 04ae962931
7 changed files with 185 additions and 6 deletions

View File

@ -413,6 +413,34 @@ func DeleteModPackHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Error creating delete modpack response: %s", err)
}
}
func LoadModPackHandler(w http.ResponseWriter, r *http.Request) {
var err error
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
name := r.FormValue("name")
resp.Data, err = loadModPack(name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp.Data = fmt.Sprintf("Error loading modpack file: %s", err)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error loading modpack: %s", err)
}
return
}
resp.Success = true
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error creating loading modpack response: %s", err)
}
}
/*func DeleteModPack(w http.ResponseWriter, r *http.Request) {
var err error
resp := JSONResponse{

View File

@ -654,3 +654,70 @@ func deleteModPack(mod_name string) (ModPackList, error) {
return mod_pack_list, nil
}
func loadModPack(name string) ([]ModInfo, error) {
var err error
zip_rc, err := zip.OpenReader(config.FactorioModPackDir + "/" + name + ".zip")
if err != nil {
log.Printf("Error opening ModPack zip: %s", err)
return nil, err
}
defer zip_rc.Close()
//delete current mods
factorio_mods_dir, err := os.Open(config.FactorioModsDir)
if err != nil {
log.Printf("Error opening factorioModsDir: %s", err)
return nil, err
}
defer factorio_mods_dir.Close()
folder_names, err := factorio_mods_dir.Readdirnames(-1)
if err != nil {
log.Printf("Error on reading dirnames: %s", err)
return nil, err
}
for _, name := range folder_names {
err = os.RemoveAll(filepath.Join(config.FactorioModsDir, name))
if err != nil {
log.Printf("Removing everything in the dir failed: %s", err)
return nil, err
}
}
//unpack zip to mods-directory
for _, single_file := range zip_rc.File {
log.Printf("filename: %s", single_file.Name)
new_file, err := os.Create(config.FactorioModsDir + "/" + single_file.Name)
if err != nil {
log.Printf("error when creating new file in mod-dir: %s", err)
return nil, err
}
singe_file_rc, err := single_file.Open()
if err != nil {
log.Printf("error when opening file inside zip: %s", err)
return nil, err
}
_, err = io.Copy(new_file, singe_file_rc)
new_file.Close()
singe_file_rc.Close()
if err != nil {
log.Printf("error when copying into the new file: %s", err)
return nil, err
}
}
//read the new mod configuration
mod_info_list, err := listInstalledModsByFolder()
if err != nil {
log.Printf("error when listing mods after extracting a ModPack: %s", err)
return nil, err
}
return mod_info_list.Mods, nil
}

View File

@ -298,6 +298,11 @@ var apiRoutes = Routes{
"POST",
"/mods/packs/create",
CreateModPackHandler,
}, {
"LoadModPack",
"POST",
"/mods/packs/load",
LoadModPackHandler,
}, {
"GetServerSettings",
"GET",

View File

@ -3,6 +3,7 @@ import ModSearch from './search/ModSearch.jsx';
import ModUpload from "./ModUpload.jsx";
import ModManager from "./ModManager.jsx";
import ModPacks from "./packs/ModPackOverview.jsx";
import {instanceOfModsContent} from "./ModsPropTypes.js";
class ModOverview extends React.Component {
constructor(props) {
@ -95,12 +96,14 @@ class ModOverview extends React.Component {
}
ModOverview.propTypes = {
// installedMods: React.PropTypes.array,
installedMods: React.PropTypes.array,
submitFactorioLogin: React.PropTypes.func.isRequired,
// toggleMod: React.PropTypes.func.isRequired,
// deleteMod: React.PropTypes.func.isRequired,
// updateMod: React.PropTypes.func.isRequired,
// uploadModSuccessHandler: React.PropTypes.func.isRequired,
toggleMod: React.PropTypes.func.isRequired,
deleteMod: React.PropTypes.func.isRequired,
updateMod: React.PropTypes.func.isRequired,
uploadModSuccessHandler: React.PropTypes.func.isRequired,
modContentClass: instanceOfModsContent.isRequired,
};
export default ModOverview;

View File

@ -0,0 +1,22 @@
import ModsContent from "../ModsContent.jsx";
function instanceOfModsContentFunction(isRequired) {
return function(props, propName, componentName) {
if(props[propName]) {
if(!props[propName] instanceof ModsContent) {
return new Error(propName + ' in ' + componentName + ' is not an instance of ModContent');
}
} else {
if(isRequired) {
return new Error(propName + ' in ' + componentName + ' is missing');
}
}
return null;
}
}
const instanceOfModsContent = instanceOfModsContentFunction(false);
instanceOfModsContent.isRequired = instanceOfModsContentFunction(true);
export {instanceOfModsContent};

View File

@ -1,6 +1,7 @@
import React from 'react';
import ModManager from "../ModManager.jsx";
import NativeListener from 'react-native-listener';
import {instanceOfModsContent} from "../ModsPropTypes.js";
class ModPackOverview extends React.Component {
constructor(props) {
@ -8,6 +9,7 @@ class ModPackOverview extends React.Component {
this.createModPack = this.createModPack.bind(this);
this.deleteModPack = this.deleteModPack.bind(this);
this.loadModPack = this.loadModPack.bind(this);
this.state = {
listPacks: []
@ -130,6 +132,54 @@ class ModPackOverview extends React.Component {
});
}
loadModPack(e) {
e.stopPropagation();
let this_class = this
let name = $(e.target).parent().prev().html();
swal({
title: "Are you sure?",
text: "This operation will replace the current installed mods with the mods out of the selected ModPack!",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true
},
function() {
$.ajax({
url: "/api/mods/packs/load",
method: "POST",
data: {name: name},
dataType: "JSON",
success: (data) => {
swal({
title: "ModPack loaded!",
type: "success"
});
// console.log(data);
this_class.props.modContentClass.setState({
installedMods: data.data
});
},
error: (jqXHR, status, err) => {
console.log('api/mods/packs/load', status, err.toString());
let json_response = jqXHR.responseJSON || err.toString();
json_response = json_response.data || err.toString();
swal({
title: "Error on loading ModPack",
text: json_response,
type: "error"
});
}
})
});
}
test() {
console.log("test called");
}
@ -147,6 +197,9 @@ class ModPackOverview extends React.Component {
<i className="fa fa-plus"></i>
<h3 className="box-title">{modpack.name}</h3>
<div className="box-tools pull-right">
<NativeListener onClick={this.loadModPack}>
<button className="btn btn-box-tool btn-default" style={{marginRight: 10}}>Load ModPack</button>
</NativeListener>
<NativeListener onClick={this.deleteModPack}>
<button className="btn btn-box-tool btn-danger" style={{color: "#fff"}}>Delete</button>
</NativeListener>
@ -179,7 +232,7 @@ class ModPackOverview extends React.Component {
}
ModPackOverview.propTypes = {
modContentClass: instanceOfModsContent.isRequired,
};
export default ModPackOverview

View File

@ -365,6 +365,7 @@ class ModsContent extends React.Component {
deleteMod={this.deleteModHandler}
updateMod={this.updateModHandler}
uploadModSuccessHandler={this.uploadModSuccessHandler}
modContentClass={this}
/>
</section>
</div>