📄 pluginupdateplugin.java
字号:
}
if ( selected_platform != null ){
String[] options = new String[selected_sub_platforms.size()];
selected_sub_platforms.toArray( options );
if ( options.length == 1 ){
selected_platform += "_" + options[0];
log.log( LoggerChannel.LT_INFORMATION,
"platform is '" + selected_platform +"'" );
}else if ( options.length > 1 ){
String selected_sub_platform = (String)
update.getDecision(
UpdateManagerDecisionListener.DT_STRING_ARRAY_TO_STRING,
"Select Platform",
"Multiple platform options exist for this plugin, please select required one",
options );
if ( selected_sub_platform == null ){
throw( new Exception( "Valid sub-platform selection not selected" ));
}else{
selected_platform += "_" + selected_sub_platform;
log.log( LoggerChannel.LT_INFORMATION,
"platform is '" + selected_platform +"'" );
}
}
}
if ( common_prefix != null ){
int pos = common_prefix.lastIndexOf("/");
if ( pos == -1 ){
common_prefix = "";
}else{
common_prefix = common_prefix.substring(0,pos+1);
}
zis = new ZipInputStream(
new BufferedInputStream( new FileInputStream( target ) ));
try{
while( true ){
ZipEntry entry = zis.getNextEntry();
if ( entry == null ){
break;
}
String name = entry.getName();
OutputStream entry_os = null;
File initial_target = null;
File final_target = null;
boolean is_plugin_properties = false;
try{
if ( name.length() >= common_prefix.length() &&
!name.endsWith("/")){
boolean skip_file = false;
String file_name = entry.getName().substring( common_prefix.length());
if ( selected_platform != null ){
if ( file_name.indexOf( "platform/" ) != -1 ){
String bit_to_remove = "platform/" + selected_platform;
int pp = file_name.indexOf( bit_to_remove );
if ( pp != -1 ){
file_name = file_name.substring(0,pp) + file_name.substring(pp+bit_to_remove.length()+1);
}else{
// stuff from other platform, ignore it
skip_file = true;
}
}
}
initial_target = new File( plugin.getPluginDirectoryName() + File.separator + file_name );
final_target = initial_target;
if ( initial_target.exists()){
if ( file_name.toLowerCase().endsWith(".properties") ||
file_name.toLowerCase().endsWith(".config" )){
is_plugin_properties = file_name.toLowerCase().equals("plugin.properties");
String old_file_name = file_name;
file_name = file_name + "_" + target_version;
final_target = new File( plugin.getPluginDirectoryName() + File.separator + file_name );
log.log( LoggerChannel.LT_INFORMATION,
"saving new file '" + old_file_name + "'as '" + file_name +"'" );
}else{
log.log( LoggerChannel.LT_INFORMATION,
"overwriting '" + file_name +"'" );
// back up just in case
File backup = new File( initial_target.getParentFile(), initial_target.getName() + ".bak" );
if ( backup.exists()){
backup.delete();
}
if ( !initial_target.renameTo( backup )){
log.log( LoggerChannel.LT_INFORMATION,
" failed to backup '" + file_name +"'" );
}
}
}
if ( !skip_file ){
final_target.getParentFile().mkdirs();
entry_os = new FileOutputStream( final_target );
}
}
long rem = entry.getSize();
byte[] buffer = new byte[65536];
while( rem > 0 ){
int len = zis.read( buffer, 0, buffer.length>rem?(int)rem:buffer.length);
if ( len <= 0 ){
break;
}
rem -= len;
if ( entry_os != null ){
entry_os.write( buffer, 0, len );
}
}
}finally{
if ( entry_os != null ){
entry_os.close();
}
}
if ( is_plugin_properties ){
// we've got to modify the plugin.version in the existing
// file (if it exists) otherwise we keep downloading the new
// version! Or, if the key doesn't exist, add it!
// if we were smarter we could merge values from the
// old one into the new one, however this is too much like
// hard work
// hmm, we really need to at least merge in the new
// predefined values such as
// plugin.name, plugin.names
// plugin.class, plugin.classes
// plugin.langfile
Properties old_props = new Properties();
Properties new_props = new Properties();
List props_to_delete = new ArrayList();
Map props_to_replace = new HashMap();
Map props_to_insert = new HashMap();
try{
FileInputStream fis = new FileInputStream( initial_target );
old_props.load( fis );
try{
fis.close();
}catch( Throwable e ){
}
fis = new FileInputStream( final_target );
new_props.load( fis );
try{
fis.close();
}catch( Throwable e ){
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}
new_props.put( "plugin.version", target_version );
String[] prop_names =
{ "plugin.name", "plugin.names",
"plugin.class", "plugin.classes",
"plugin.version",
"plugin.langfile"
};
for (int z=0;z<prop_names.length;z++){
String prop_name = prop_names[z];
String old_name = old_props.getProperty( prop_name );
String new_name = new_props.getProperty( prop_name );
if ( new_name != null ){
if ( prop_name.equals( "plugin.name")){
props_to_delete.add( "plugin.names" );
}else if ( prop_name.equals( "plugin.names")){
props_to_delete.add( "plugin.name" );
}else if ( prop_name.equals( "plugin.class")){
props_to_delete.add( "plugin.classes" );
}else if ( prop_name.equals( "plugin.classes")){
props_to_delete.add( "plugin.class" );
}
if ( old_name == null ){
props_to_insert.put( prop_name, new_name );
}else if ( !new_name.equals( old_name )){
props_to_replace.put( prop_name, new_name );
}
}
}
File tmp_file = new File(initial_target.toString() + ".tmp");
File bak_file = new File(initial_target.toString() + ".bak");
LineNumberReader lnr = null;
PrintWriter tmp = null;
try{
lnr = new LineNumberReader(new FileReader(initial_target));
tmp = new PrintWriter(new FileWriter( tmp_file ));
Iterator it = props_to_insert.keySet().iterator();
while( it.hasNext()){
String pn = (String)it.next();
String pv = (String)props_to_insert.get(pn);
log.log(" Inserting property:" + pn + "=" + pv );
tmp.println( pn + "=" + pv );
}
while(true){
String line = lnr.readLine();
if ( line == null ){
break;
}
int ep = line.indexOf('=');
if( ep != -1 ){
String pn = line.substring(0,ep).trim();
if ( props_to_delete.contains(pn)){
log.log(" Deleting property:" + pn );
}else{
String rv = (String)props_to_replace.get( pn );
if ( rv != null ){
log.log(" Replacing property:" + pn + " with " + rv );
tmp.println( pn + "=" + rv );
}else{
tmp.println( line );
}
}
}else{
tmp.println( line );
}
}
}finally{
lnr.close();
if ( tmp != null ){
tmp.close();
}
}
if ( bak_file.exists()){
bak_file.delete();
}
if ( !initial_target.renameTo( bak_file)){
throw( new IOException( "Failed to rename '" + initial_target.toString() + "' to '" + bak_file.toString() + "'" ));
}
if ( !tmp_file.renameTo( initial_target )){
bak_file.renameTo( initial_target );
throw( new IOException( "Failed to rename '" + tmp_file.toString() + "' to '" + initial_target.toString() + "'" ));
}
bak_file.delete();
}else if ( final_target != null && final_target.getName().equalsIgnoreCase( "update.txt" )){
update_txt_found = true;
LineNumberReader lnr = null;
try{
lnr = new LineNumberReader( new FileReader( final_target ));
while(true){
String line = lnr.readLine();
if (line == null ){
break;
}
log.log( LoggerChannel.LT_INFORMATION, line );
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}finally{
if ( lnr != null ){
lnr.close();
}
}
}
}
}finally{
zis.close();
}
}
}
if ( unloadable ){
log.log( "Plugin initialising, please wait... " );
plugin.reload(); // this will reload all if > 1 defined
log.log( "... initialisation complete." );
}
}
String msg = "Version " + version + " of plugin '" +
plugin.getPluginID() + "' " +
"installed successfully";
if ( update_txt_found ){
msg += " - See update log for details";
}
log.logAlertRepeatable( update_txt_found?LoggerChannel.LT_WARNING:LoggerChannel.LT_INFORMATION, msg );
}catch( Throwable e ){
String msg = "Version " + version + " of plugin '" +
plugin.getPluginID() + "' " +
"failed to install - " + (e.getMessage());
log.logAlertRepeatable( LoggerChannel.LT_ERROR, msg );
}finally{
update.complete();
}
}
protected void
logMultiLine(
String indent,
List lines )
{
for (int i=0;i<lines.size();i++){
log.log( LoggerChannel.LT_INFORMATION, indent + (String)lines.get(i) );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -